문제

I use a SlidingMenu to display a Side Drawer (instead of tabs). Now, inside the SlidingMenuFragment, there is a ListView with 6 rows, each one being an equivalent of a tab. When the user touches one of the 6 items, the content view should update the Framelayout with the corresponding Fragment for that item.

Ex:

  1. Cats
  2. Dogs
  3. Animals

When you select cats, the SlidingMenu disappears and the CatsFragment should show up on the screen, replacing the old fragment present there.

Question: What is the best way to store all the fragments that are going to be swapped to and from the content view, also taking into consideration orientation change ? Instantiate a new Fragment every time I need to swap it ? Or instantiate all of them, save them in list then use them from there ?

What I did:

  1. Whenever a fragment needs swapping, I use the code below.
  2. To store the list of fragments for easy access, I use an ArrayList. I save this ArrayList in onRetainCustomNonConfiguration() and retrieve it after orientation change, so that I don't instantiate another list of fragments, but I don't believe this is a good idea.

        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.activity_main_framelayout, mFragmentsList.get(mCurrentFragmentPosition))
        .commit();
    
도움이 되었습니까?
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top