0

I'm currently using a ViewGroup (SwipeyTab implementation) to switch between Fragments. However, some Fragment "pages" get replaced by other Fragments on the same Tab, so I initially tried:

                FragmentTransaction ft = fragment.getFragmentManager().beginTransaction();
                ft.remove(currentFragment);                 
                ft.add(newFragment,"");
                ft.commit();

That code would remove the current fragment but not add newFragment (from Logcat, it would get instantiated but not appear).

I ended up adding it in the FragmentPagerAdapter.getItem(int position) call based on current state (based on this: Replace Fragment inside a ViewPager). However, I'd like to be able to add each newly replaced fragment to be part of the back stack.

I tried adding to backstack before removing the fragment:

currentFragment.getFragmentManager().beginTransaction().addToBackStack(null).commit(); FragmentTransaction ft = fragment.getFragmentManager().beginTransaction(); ft.remove(currentFragment);
ft.commit(); and that didn't work - it added the last fragment to the backstack, so when I pressed back, it would just reload the current fragment.

Is there anyway I can add a fragment to the backstack that has been replaced in the "non traditional" way?

Community
  • 1
  • 1
StackOverflowed
  • 5,854
  • 9
  • 55
  • 119
  • The UX you are describing does not make sense IMHO. The user brings up your activity. The user does something to cause you to "add [a] newly replaced fragment to be part of the back stack". The user then spends time swiping around the pager. Now, the user presses BACK. You are proposing that this does not leave the activity, but rather pops some `FragmentTransaction` from a while back. IMHO, `ViewPager` contents are independent of the back stack, just as tabs are independent of the back stack. – CommonsWare May 04 '12 at 11:50
  • Thanks for your feedback. I'm not actually concerned about what happens after the user swipes -- it's after being on the same Tab, but having had 1 or 2 fragments replaced. So let's say Page 1 has FragmentA replaced by FragmentB replaced by FragmentC. While on FragmentC, if user presses Back, I want to go back to FragmentB. However, in the current implementation, it's actually leaving the entire activity. – StackOverflowed May 05 '12 at 12:37
  • That above comment was for @CommonsWare – StackOverflowed May 05 '12 at 13:13

0 Answers0