How to open another Activity from a Navigation Drawer?

Asked

Viewed 3,938 times

3

I need to call a Activity from an item of a navigation drawer. Anyone can help?

These are the items I want to call for Activitys:

     public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {



    } else if (id == R.id.nav_programacao) {

    } else if (id == R.id.nav_galeria) {

    } else if (id == R.id.nav_visitar_site) {

    } else if (id == R.id.nav_instagran) {

    } else if (id == R.id.nav_facebook) {

    } else if (id == R.id.nav_twitter) {



    }
  • You know how to open other Activitys and is having trouble just with this case or you do not know how to open Activitys?

  • can’t open yet

4 answers

3

To call a new Activity():

Create a new Activity by the name of MainActivity2 and insert this code below into your method onNavigationItemSelected of his MainActivity:

Intent intent = new Intent(this, MainActivity2.class); 
startActivity(intent);

To call a new Fragment(Fragment):

Create a new Fragment by the name of novoFragment and insert this code below into your method onNavigationItemSelected of his MainActivity:

novoFragment fragment = new novoFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();

** Important Notice **

Why did you call fragment if it concludes successfully (work), it is necessary to follow this more complete post on the subject: 3-point menu in all Activities with standard Navigation Drawer


In Your Mainactivity - Solution in Practice:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // abrindo um novo fragment
     novoFragment fragment = new novoFragment();
    android.support.v4.app.FragmentTransaction fragmentTrasaction =
    getSupportFragmentManager().beginTransaction();
    fragmentTrasaction.replace(R.id.fragment_container, fragment);
    fragmentTrasaction.commit();
    } else if (id == R.id.nav_gallery)  {
    // abrindo um nova activity
    Intent intent = new Intent(this, MainActivity2.class); 
    startActivity(intent);    
    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

2

Try so Marcos, in the example below you try to check if there is the app, passing the package name:

this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

or in this other example you let the user choose which app to use, you just send a text.

Intent it = new Intent();
it.setAction(Intent.ACTION_SEND);
it.putExtra(it, "SUA MENSAGEM");
it.setType("text/plain");

1


Supposing that the Activity that you want to open is called MinhaAtividade:

Intent intent = new Intent(this, MinhaAtividade.class);
startActivity(intent);

this here is the context of Intent. I’m assuming you’re inside a Activity or another subclass of Context. If not, adapt this parameter. The same goes for startActivity.

  • Pablo, my problem is what I call Activity when you click on any of these items, which event I use.

  • Judging by your ifs and your response, it looked like you were past that part. Put more of your code then, and explain better what the problem is.

  • Suppose I choose the nav_programming item, when I clicked I wanted it to call to Activity programming, that’s the context of my problem, entedeu?

  • It would be interesting to see at least the code that surrounds those ifs of yours, to avoid writing another answer that is not what you want.

  • to better visualize I put in the main question...

  • So, you already have the click response method. Just put the code similar to the one I wrote, replacing the MinhaAtividade for Activity that you want to open in each of the cases.

  • understood.. I resolved

Show 2 more comments

0

Create a method and call the Activitys you want:

private void callActivity(Class c){
    Intent it = new Intent(this, c);
    startActivity(it);
}
  • Note that this method only works if you are inside a Context

  • Arthur, take a look at the code in the main question

  • Sorry Marcos, let me see if I understand now, you would like to call for example nav_twitter an link that opens directly the Twitter app? That’s it?

  • yes, Arthur....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.