简体   繁体   中英

Navigating through Fragments in android without adding it into backstack

I'm working with android applications. I'm facing the issue like fragments adding in the back stack History. Let Me explain clearly, I want to navigate from Fragment A to Fragment B, then come back to Fragment A using button click in the Fragment B. Above scenario working good, after this navigation to Fragment A when i click back arrow in my device it navigates to the Fragment B. I don't want this to happen. Give me a solution. My code structure is below:

Fragment A:

.......
FragmentB fragmentB = new FragmentB();
showFragmentOmitStack(fragmentB);
....


public void showFragmentOmitStack(Fragment fragment) {
 FragmentTransaction transaction = getFragmentManager().beginTransaction();
 transaction.add(R.id.container,fragment);//add // replace
 transaction.addToBackStack(null);
 transaction.commit();
}

Fragment B:

FragmentA fragmentA = new FragmentA();
showFragment(fragmentA, "FragmentA");


public void showFragment(Fragment fragment, String back_stack_name) {
 FragmentManager fragmentManager = thisActivity.getFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.add(R.id.container, fragment);
 fragmentTransaction.addToBackStack(back_stack_name);
 fragmentTransaction.commit();
}

In My Activity java file:

@Override
public void onBackPressed() {
 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
 if (drawer.isDrawerOpen(GravityCompat.START)) {
     drawer.closeDrawer(GravityCompat.START);
   } else {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
      showAlertDialog(Constants.EXIT_APP, Constants.APP_NAME);
     } else {
        getFragmentManager().popBackStackImmediate();
            }
        }
    }

Try this:

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
    }else {
       if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
           drawerLayout.closeDrawer(GravityCompat.START);
         } else {
            super.onBackPressed();
        }  
    }
}

If you don't intend to go back to the previous fragment with a back press just don't add the fragment to the backStack when you add the fragment, ie remove this:

fragmentTransaction.addToBackStack(myFragmentName);

Each fragment has it's own transaction, so when you add A with addToBackStack , thats reffering to fragment A! In fragment B transaction you set it to null, you are saying that B won't go in the backs tack. So the only fragment in the back stack that will be affected by the back press is A!

On the button click event that you have set on Fragment B that moves you back to Fragment A from there just remove the

yourTransaction.addToBackStack(null);

And you shall be Good to go.

If you're working with only two Fragments, A and B, then you could do something like this:

In your Activity

@Override
public void onBackPressed() {
  if (drawer.isDrawerOpen(GravityCompat.START)) {
    drawer.closeDrawer(GravityCompat.START);
  } 
  else if (currentFragment instanceOf FragmentB) {
    Fragment fragmentA = new FragmentA();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = 
    fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, fragmentA);
    fragmentTransaction.commit();
    currentFragment = fragmentA;
  }  
  else {
       super.onBackPressed();
      }
  }
}

Instead of adding to the back stack, just create an instance variable in your class: Fragment currentFragment = null; . And then just keep setting currentFragment to the fragment you started.

addToBackStack(null) still adds Fragment B to the back stack. The null parameter is just the name of the back stack state. You should remove that line in your showFragmentOmitStack method if you don't want the fragment to be added to the back stack.

Source: https://developer.android.com/reference/android/app/FragmentTransaction#addToBackStack(java.lang.String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM