I'm trying to implement the following fragment design:
Fragment A is replaced by fragment B, which in turn, is replaced by fragment C.
Whether in fragment B or C, I want the user back navigation to take it back to fragment A.
I add to backstack when replacing A with B. But when I move from B to C, I don't add to backstack.
When navigating back from fragment B, everything works fine.
But, when navigating back from C, I get A and C on the same screen - C doesn't disappear.
I wonder if it is related to my backstack usage.
Any help is appreciated.
My code is equivalent to:
Fragment fragment;
fragment = new FragmentA();
transaction.replace(R.id.container, fragment);
transaction.commit();
fragment = new FragmentB();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
fragment = new FragmentC();
transaction.replace(R.id.container, fragment);
transaction.commit();
This is the general way to add to Backstack. Use tags.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.fragment_container, fragment1, Fragment1.class.getName());
fragmentTransaction.addToBackStack(Fragment1.class.getName());
fragmentTransaction.commit();
Now similarly, for frament2:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment_container, fragment2, Fragment2.class.getName());
fragmentTransaction.addToBackStack(Fragment2.class.getName());
fragmentTransaction.commit();
Do the same for fragment3.
And now you remove fragment2 from backstack using:
getFragmentManager().popBackStack(
Fragment2.class.getName(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
You should be able to skip directly from fragment3 back to fragment2.
Also, are you using fragmentTransaction.replace(...) and not fragmentTransaction.add(...). Could you post relevant code?
Related
I have implemented fragments in my application. Here my code for swiching fragment in fragment_container.
private void switchFragment(Fragment fragment, boolean isAddToBackStack, String tag)
{
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment, tag);
if (isAddToBackStack)
ft.addToBackStack(tag);
setCurrentTopFragment(Integer.parseInt(tag));
ft.commit();
}
I have 4 fragments A,B,C and D and for switching between this fragmnets I am using above method. I have A,C,B in my backstack. If again I switch to fragmnet A, my backstack is like A,C,B,A. What I actually want is If I swich to A again I want backstack sequence like this C,B,A. Means Remove old instance from backstack and add new to it.
First get the back-stacked Fragment by id which you need to remove:
Fragment fragment = getSupportFragmentManager().getFragment(new Bundle(), TAG_KEY)
or there are several methods getBackStackEntryCount(), getBackStackEntryAt. After getting the fragment which you need to remove. Remove it from the fragment back-stack.
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(fragment);
Then you can add a new fragment Done :)
I have coded like this:
FragA >> FragB >> FragC >> FragD
When I press onBackpress() of FragD it goes to directly on FragA but i want to keep it go FragC.
BackPress Code here :
if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0) {
back_btn.setVisibility(View.GONE);
getActivity().getSupportFragmentManager().popBackStack();
}
Replacing Fragment Code :
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA fragmentA = new FragmentA();
fragmentTransaction.replace(R.id.framelayoutinner, fragmentA);
fragmentTransaction.addToBackStack("fragmentA");
fragmentTransaction.commit();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentA fragmentB = new Fragmentb();
fragmentTransaction.replace(R.id.framelayoutinner_2, fragmentB);
fragmentTransaction.addToBackStack("fragmentB");
fragmentTransaction.commit();
You are using nested fragments, if you load fragment from another fragment then it become nested structure. So try to check if any child fragment exist within your Root fragment then pop that child fragment first.
Use getChildFragmentManager() for that and make recursive calls till you get most young child fragment(Last added). Or better use Tag on your fragments.
You need to add every fragment into backstack using addToBackStack method
public void setmFragmentContainer(Fragment fragment)
{
final String tag = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout_content, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}
This method may help you
I have 1 activity with multiple fragments. These fragments call each other, for example fragment a calls fragment b:
Fragment a:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new FragmentB());
ft.commit();
Where OrderAddFragment is fragment b.
Now, let's say a user has filled in multiple inputs in fragment a and continues to fragment b but than he realises he made a mistake in fragment a and needs to go back to correct this mistake.
How could I reopen fragment a with the saved instance state instead of creating a new instance?
The back function in fragment b uses following code to reopen fragment a:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new FragmentA());
ft.commit();
FragmentTransaction.addToBackStack() might solve your problem.
I have 4 Fragments. A B C D
I have kept only fragment A in backstack.
I go from fragment A -> B -> C -> D
now on backpress in fragment D, I should redirect to fragment A but rite now both A and D fragment gets displayed on screen together.
How do I hide fragment D?
What if you popbackstack ..adding fragment A and launch any fragment..
for example:
Fragment fragmentA = new FragmentA();
Fragment fragmentD = new FragmentD();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
manager.popBackStack();
transaction.addToBackStack(fragmentA.getClass().getName());
transaction.replace(R.id.frame_container, fragmentD);
transaction.commit();
Step 1 : Write code in Main Activity who contains Frame Layout it's name is fragcontainer
getSupportFragmentManager().beginTransaction().add(R.id.frgContainer,new FragOne(),FragOne.class.getName()).commit();
Write Code on "FragOne"
FragThird FragThird = new FragThird();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
// ft.addToBackStack(FragTrackView.class.getName());
ft.add(R.id.frgContainer, FragThird, FragFourth.class.getName());
ft.commit();
same it is add 2 Fragment and Last Fragment add like this
FragFourth FragFourth= new FragFourth();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
ft.addToBackStack(FragFourth.class.getName());
ft.add(R.id.frgContainer, FragFourth, FragFourth.class.getName());
ft.commit();
In Fragment3 have child fragment, if i open Fragment3 from Fragment1 then Fragment4 is shown but now i open fragment3 from fragment2 then fragment 4 is not shown.
And vice-versa if i open Fragment 3 from Fragment 2 then Fragment4 is shown, but if i open Fragment 3 from Fragment1 then Fragment 4 is not shown.
Code to add Fragment 4:
Fixture_H2h_frag fragment1=new Fixture_H2h_frag();
Bundle b1 = new Bundle();
b1.putBoolean("Show", true);
//b.putString("RESULT", result);
b1.putString("URL", Url);
b1.putString("TYPE", "FORM");
b1.putString("VisitorTeam1", matchlist.get(pos).getVisitor_image_url());
b1.putString("LocalTeam1", matchlist.get(pos).getLocal_image_url());
fragment1.setArguments(b1);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.Inner_Fixture_Container, fragment1);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Code to add Parent Fragment:
FixtureDescrption_Frag frag = new FixtureDescrption_Frag();
FragmentManager fm = contxt.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place, frag);
fragmentTransaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
if(addedtostack){
fragmentTransaction.addToBackStack(contxt.getClass().getName());
}
fragmentTransaction.commit();
You need to use
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
instead of
FragmentTransaction transaction = getFragmentManager().beginTransaction();
While adding child fragment.
You can refer this link for more details.