1

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?

edovino
  • 3,315
  • 2
  • 22
  • 22
  • set other fragments `addToBackstack(null)`, in your case B, C and D – Murtaza Khursheed Hussain Feb 02 '15 at 12:03
  • possible duplicate of [How to keep only first added Fragment in back stack (fragment overlapping)?](http://stackoverflow.com/questions/14269350/how-to-keep-only-first-added-fragment-in-back-stack-fragment-overlapping) – Budius Feb 02 '15 at 12:06
  • when you are moving from fragment A to B and B to C and C to D do not use addToBackstack(null) as it push the fragment into stack only use it the first time when you are on fragment A, hope it helps – Syed Raza Mehdi Feb 02 '15 at 12:18
  • Tried doing addToBackStack(null) but it goes back from D to C , from C to B and from B to A – user3237043 Feb 02 '15 at 12:18
  • Tried addToBackStack(null) only once for the first time but onBackPress from D, it displays both together D and A on screen – user3237043 Feb 02 '15 at 12:24
  • Well if nothing else goes well...just putt Fragment Transaction in the method onBackPress to always go to Fragment A. – David Kasabji Feb 02 '15 at 13:17
  • hey @user3237043 check my previous answer http://stackoverflow.com/questions/14269350/how-to-keep-only-first-added-fragment-in-back-stack-fragment-overlapping that explains why it's happening and points to possible workarounds. Short answer is that FragmentManager is kinda of stupid. – Budius Feb 02 '15 at 13:20

2 Answers2

0

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();
J.Vassallo
  • 2,282
  • 3
  • 15
  • 14
0

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();
Bipin Bharti
  • 1,034
  • 2
  • 14
  • 27
  • ft.addToBackStack(FragFourth.class.getName()); this line add the Fragment in back stack if you dont't require then comment or remove that line – Bipin Bharti Dec 19 '16 at 06:10