-1

Je voudrais quand j'ouvre le menu du panneau de navigation et cliquez sur un menu particulier, il peut ouvrir une activité au lieu d'un fragmentComment ouvrir une activité d'un clic de menu Tiroir de navigation au lieu de Fragment

Voici mon code , certains d'entre eux sont liés à des fragments et ceux qui sont vides, je voudrais qu'ils ouvrent une activité

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

    if (id == R.id.nav_home) { 
     //Set the fragment initially 
     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, new TabFragment()).commit(); 

    } else if (id == R.id.nav_gallery) { 
     //Set the fragment initially 
     FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, new TabFragment2()).commit(); 

    } else if (id == R.id.nav_nearby) { 
     //Set the fragment initially 

    } else if (id == R.id.nav_search) { 
     //Set the fragment initially 

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

    } else if (id == R.id.nav_explore) { 
     //Set the fragment initially 

    } else if (id == R.id.nav_getverified) { 
     //Set the fragment initially 

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

    } else if (id == R.id.nav_getfeatured) { 
     //Set the fragment initially 

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

    } else if (id == R.id.nav_register) { 
     //Set the fragment initially 

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

    } else if (id == R.id.nav_login) { 
     //Set the fragment initially 

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

    } else if (id == R.id.nav_report) { 
     //Set the fragment initially 

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

    } 

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

Par exemple dans le menu de recherche Je voudrais lier à une activité.

Répondre

0

D'abord, vous devez nettoyer votre code. Vous répétez l'instanciation de FragmentTransaction dans chaque élément de lien.

Vous pouvez ajouter ce qui suit en tant que variables membres de classe dans votre classe d'activité par exemple

public class MainActivity extends AppCompatActivity { 

    private Fragment fragment; 
    private FragmentTransaction fragmentTransaction; 

Je nettoyer votre méthode onNavigationItemSelected (point MenuItem) et a également ajouté la façon dont vous pouvez ajouter la page d'activité au point de navigation.

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

    if (id == R.id.nav_home) { 
     //Set the fragment initially 
     fragment = new TabFragment(); 

    } else if (id == R.id.nav_gallery) { 
     //Set the fragment initially 
     fragment = new TabFragment2(); 

    } else if (id == R.id.nav_nearby) { 
     //Set the fragment initially 
     Intent intent = new Intent(YourActivity.this, NearByActivity.class); 
     startActivity(intent); 
     finish(); 

    } else if (id == R.id.nav_search) { 
     //Set the fragment initially 
     fragment = new Search(); 

    } else if (id == R.id.nav_explore) { 
     //Set the fragment initially 
     Intent intent = new Intent(YourActivity.this, ExploreActivity.class); 
     startActivity(intent); 
     finish(); 

    } else if (id == R.id.nav_getverified) { 
     //Set the fragment initially 
     fragment = new GetVerified(); 

    } else if (id == R.id.nav_getfeatured) { 
     //Set the fragment initially 
     fragment = new GetFeatured(); 

    } else if (id == R.id.nav_register) { 
     //Set the fragment initially 
     fragment = new Register(); 

    } else if (id == R.id.nav_login) { 
     //Set the fragment initially 
     fragment = new Login(); 

    } else if (id == R.id.nav_report) { 
     //Set the fragment initially 
     fragment = new Report(); 
    } 

    fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_container, fragment).commit(); 

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

Salut, merci beaucoup, mais je reçois une erreur "ne peut pas résoudre logoutIntent" –

+0

J'ai mis à jour la réponse – Inducesmile

+0

Merci beaucoup! cela a fonctionné parfaitement –

0

Vous pouvez facilement démarrer une intention par: `

startActivity(new Intent(this, ActivityToStart.class) 

dans chaque instruction if où vous voulez commencer une autre activité depuis votre tiroir de navigation se trouve dans votre classe d'activité.

Vous pouvez également écrire le code de remplacement de fragment dans une fonction distincte afin de créer du code réutilisable plutôt que de l'écrire pour chaque clic d'élément.

J'espère que ça aide!