0

My Android app has only one activity and all the fragments are added or replaced in one framelayout.

The issue is when I have two different viewpagers in the fragment backstack and the activity is recreated the second viewpager comes up as the first viewpager.

Below is how I add the fragments to the backstack in the order that they happen so 3 is the fragment that comes up after the activity is recreated.

  1. setupPlacesEventMainView(), this is a viewpager which uses a FragmentStatePagerAdapter.(shows up properly after activity is recreated)
  2. setupPlaceDetailViewPager, this is a viewpager which uses a FragmentStatePagerAdapter. (ISSUE: PlacesEventMainViewPager shows up here instead of PlaceDetailViewPager. The issue occurs even if I don't navigate into the EventSpecials)
  3. setupEventSpecials(), this is a listfragment. (shows up properly after activity is recreated)

Add PlacesEventMainViewPager to backstack

public void setupPlacesEventMainView()
{
    clearBackStack();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    PlacesEventMainViewPager placesEventMainViewPager = new PlacesEventMainViewPager();
    transaction.replace(R.id.fragment_container, placesEventMainViewPager, PlacesEventMainViewPager.class.getSimpleName());
    transaction.commit();
}

Add PlaceDetailViewPager to backstack

 public void setupPlaceDetailViewPager(Event event) {


    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    PlaceDetailViewPager placeDetailViewPager = new PlaceDetailViewPager();
    placeDetailViewPager.setEvent(event);
    transaction.add(R.id.fragment_container, placeDetailViewPager,PlaceDetailViewPager.class.getSimpleName());
    transaction.addToBackStack(PlaceDetailViewPager.class.getSimpleName());
    transaction.commit();
}

Add SpecialsList to backstack

    public void setupEventSpecials(Agenda[] agendas) {

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    SpecialsList specialsList = new SpecialsList();
    specialsList.setAgendas(agendas);
    transaction.add(R.id.fragment_container, specialsList,SpecialsList.class.getSimpleName());
    transaction.addToBackStack(SpecialsList.class.getSimpleName());
    transaction.commit();
}

What the fragments contain.

  • PlacesEventMainViewPager contains 3 listfragments.
  • PlaceDetailViewPager contains 2 listfragments
  • SpecialsList is 1 listfragment.

I can reach the PlaceDetailViewPager fragment by clicking on one of the items in the PlacesEventMainViewPager fragment. I can reach the SpecialsList by clicking a button in the PlaceDetailViewPager fragment.

Please let me know if you need any more details. Thanks!

1 Answers1

0

You never add PlacesEventMainViewPager to the backstack which makes PlaceDetailViewPager the first one in the backstack if you listed them in order.

young_souvlaki
  • 1,886
  • 4
  • 24
  • 28