J'ai deux fragments savoir FoodFragment
et WishlistFragment
ces deux sont ouverts lorsque je presse son article clique sur le NavigationView
Impossible de trouver le fragment par étiquette lorsque le bouton est pressé Retour
respective Le problème que je suis face est quand je appuyez sur les éléments de navigation les fragments correspondant à eux s'ouvre et tout fonctionne bien mais quand j'appuie sur le bouton de retour, la méthode onBackPressed()
MainActivity est appelée et cela ne fonctionne que pour la première condition sur le fragment que j'ai écrit.
Voici mon code:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
mFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (id == R.id.food) {
if(newFragment == null) {
newFragment = FoodFragment.newInstance("food", "fragment");
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.appBar, newFragment, "FoodFragment").addToBackStack("FoodFragment").commit();
}
}else if(id == R.id.wishlist){
if(wishFragment == null){
wishFragment = WishlistFragment.newInstance("wish", "fragment");
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.appBar, wishFragment, "WishFragment").addToBackStack("WishFragment").commit();
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
est ici la méthode onBackPressed():
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
try {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if(getSupportFragmentManager().findFragmentByTag("WishFragment").isVisible()){
getSupportFragmentManager().popBackStack();
wishFragment = null;
}else if (getSupportFragmentManager().findFragmentByTag("FoodFragment").isVisible()) {
getSupportFragmentManager().popBackStack();
newFragment = null;
}else {
super.onBackPressed();
}
}catch (NullPointerException npe){
super.onBackPressed();
}
}
J'ai ajouté des balises à chaque fragment comme vous pouvez le voir dans le code et les manipuler à travers le onBackPressed()
méthode. Ici, quand j'ouvre le WishFragment
et que j'appuie sur le bouton de retour, tout est effacé et je peux à nouveau ouvrir le WishFragment
en appuyant sur l'élément de navigation correspondant. Mais, cela semble aller mal dans le cas de FoodFragment
lorsque j'ouvre le fragment et appuyez sur le bouton de retour, la condition correspondant à elle n'est pas exécutée et donc, je suis incapable d'ouvrir le FoodFragment
à nouveau.
Mais, si je change l'ordre des conditions dans onBackPressed()
comme ceci:
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
try {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}else if (getSupportFragmentManager().findFragmentByTag("FoodFragment").isVisible()) {
getSupportFragmentManager().popBackStack();
newFragment = null;
} else if(getSupportFragmentManager().findFragmentByTag("WishFragment").isVisible()){
getSupportFragmentManager().popBackStack();
wishFragment = null;
}else {
super.onBackPressed();
}
}catch (NullPointerException npe){
super.onBackPressed();
}
}
Il semble travailler maintenant avec FoodFragment
mais pas avec WishFragment
, par conséquent, j'ai pu ouvrir le nouveau FoodFragment
mais pas le WishFragment
. J'ai cherché ce problème sur de nombreux sites Web, mais je ne peux pas obtenir la bonne réponse.
Je l'ai résolu en ajoutant view.setOnKeyListener(//calling the MainActivity by KeyEvents)
pour le fragment mais, cela ne semble pas être une bonne façon de faire les choses car il recharge à nouveau MainActivity
entier.
Je ne sais pas où je me trompe. S'il vous plaît me suggérer une meilleure façon de le faire.
Merci.
Merci ... Cette résolu le problème –