Popbackstack But Keep The First Fragment In Android
Solution 1:
E.g. you can do following:
- add fragA without adding it to backStack. So it always be in activity, and won't react on back button.
- when you open fragD you should clear fragment BackStack. So when you press back button from D frag you go back to A.
P.S. There are other ways how to do what you want. It depends...
Solution 2:
Because the "back stack" has a stack-like behaviour...last-in, first-out...the last fragment you added to the back stack will be popped out of the back stack first. You will need to implement the behaviour you required manually by specifying your own. This is not that hard using the FragmentManager
class methods.
If you "tag" your fragments as you add them to the transaction...
fragmentTransaction.add(new FragmentA(), "FragmentA_Tag");
you can later determine which fragment to show when the back button is pressed...
FragmentAf= fragmentManager.findFragmentByTag("FragmentA_Tag");
if(f != null){
f.show();
}
How you determine which fragment to show it's entirely up to you. You can keep track of the current visible fragment or you can use the isHidden
method of the Fragment
class...BTW, I'm talking about native fragments here, not support library's fragment.
Solution 3:
The backstack contains records about transactions, not fragments itself.
So you should not add first transaction's record (null -> fragA) to backstack.
And all other transaction's record should be added to backstack.
In this case, then you preformpopBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
android removed all fragments except fragA, because there isn't any records about how fragA was added.
Solution 4:
Only a few days ago I start learning about fragments in Android. And I faced with this problem too. Here I show my solution and how I resolve this. Please, fix if my code is not right. What we have at this time? Acivity, many fragments and their backstack. We want open every fragment from Drawer menu and clear all other fragments from backstack. But, we must hold only one Home fragment. When we stay on Home fragment and user press Back button app is goeing closing.
Activity.class
protectedvoidonCreate(Bundle savedInstanceState)
{
...
// adding Home fragment without adding transaction into backstackFragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.container, HomeFragment.newInstance("args"), null);
ft.commit();
}
@OverridepublicvoidonBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
finish();
}
}
publicvoidaddFragmentFromMenu(Fragment fragment){
String backStateName = fragment.getClass().getName();
clearBackStack();
FragmentManager manager = getSupportFragmentManager();
if(manager.getBackStackEntryCount()> 0)
{
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(backStateName) == null) {
//fragment not in back stack, create it.addFragment(fragment, manager, backStateName);
}
}
else// no fragments
{
addFragment(fragment, manager, backStateName);
}
}
publicvoidaddFragment(Fragment fragment, FragmentManager manager, String backStateName)
{
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.container, fragment, backStateName);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
publicvoidclearBackStack() {
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
And on click on drawer menu item
@Override
public boolean onNavigationItemSelected(MenuItem item) {
intid = item.getItemId();
if (id == R.id.nav_camera) {
addFragmentFromMenu(CameraFragment.newInstance("cam1", "cam2"));
} elseif (id == R.id.nav_gallery) {
addFragmentFromMenu(TestFragment.newInstance("test1","test2"));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Solution 5:
After looking through many posts, I figured it out this way:
in fragC => fragD method, do two transactions:
1 clear back stacks, fragC => fragA
2 fragA => fragD
But in this way, the original state of fragA may be destroyed.
publicstaticvoidchangeFragCtoD(FragmentManager fm, Fragment fragD){
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransactionfragmentTransaction= fm.beginTransaction();
fragmentTransaction
.replace(R.id.containerViewId, newfragAClass())
.commit();
FragmentTransactionfragmentTransaction= fm.beginTransaction();
fragmentTransaction
.replace(R.id.containerViewId, fragD)
.addToBackStack(fragD.getClass().getName())
.commit();
}
Now pressing back on fragD goes back to fragA.
Post a Comment for "Popbackstack But Keep The First Fragment In Android"