How to open another Activity from a Navigation Drawer?

3

I need to call a Activity from an item of navigation drawer . Can anyone help?

These are the items I want to call for Activity s:

     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) {



    }
    
asked by anonymous 17.03.2016 / 02:32

4 answers

1

Assuming the% w / w of% you want to open is called% w /%:

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

Activity here is the context of MinhaAtividade . I'm assuming you're within this or another subclass of Intent . If not, adjust this parameter. The same thing goes for Activity .

    
17.03.2016 / 13:52
3

To call up a new Activity:

Create a new Activity with the name of MainActivity2 and enter this code below in your onNavigationItemSelected method of your MainActivity :

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

To call a new Fragment:

Create a new Fragment with the name of novoFragment and enter this code below in your onNavigationItemSelected method of your MainActivity :

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

** Important Notice **

In order for this fragment call to complete successfully (work), you need to follow this most complete post on the subject: 3-point menu in all Activities with the default 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;
}
    
12.05.2016 / 07:10
2

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");
    
17.03.2016 / 17:37
0

Create a method and call the Activities you want:

private void callActivity(Class c){
    Intent it = new Intent(this, c);
    startActivity(it);
}
    
17.03.2016 / 13:58