0

Related Question.

I put together a simple app that goes like this: Activity -> FirstFragment

Activity: onCreate() -> createFirstFragment()

    FirstFragment firstFragment = (FirstFragment) getSupportFragmentManager().findFragmentByTag(FirstFragment.TAG);
    if (firstFragment == null)
    {
        firstFragment = FirstFragment.newInstance();
        getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.firstFragmentContainer, firstFragment, FirstFragment.TAG)
            .hide(firstFragment)
            //.addToBackStack(null)
            .commit();
    }

Plain and simple, during onCreate() add and hide a fragment so that I can do show/hide animations later.

Now, my question is this: why does the Activity/FragmentManager not remember this transaction (regardless of whether I .addToBackStack() or setRetainInstance(true) on the fragment) when the activity is killed and recreated? You can test this by checking the Do not keep activities developer option. Start the app, firstFragment is hidden as expected, minimize and come back, and viola! firstFragment is there for all the world to see!

I would expect that this sort of thing would be managed by Android, or do I need to specifically record all my transactions and repeat them when the app is recreated?

Any help/advice would be greatly appreciated!

Edit: Also see related logged bug

Community
  • 1
  • 1
logan
  • 430
  • 5
  • 11

1 Answers1

0

Use FragmentStatePagerAdapter like below in your main activity. This internally calls 'onSaveInstanceState' of the fragments and hence keeps the track of the changes you made and retains the transactional states

class MyAdapter extends FragmentStatePagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // your code here
    }

    @Override
    public int getCount() { 
           // returns no. of fragments count. in my case it is 4
        return 4;
    }

onCreate() in mainactivity generally looks like this:

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView (R.layout.scrollabletabs_main);
        viewPager = (ViewPager) findViewById (R.id.pager);
        FragmentManager fragManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragManager));
    }

From http://developer.android.com/training/basics/fragments/fragment-ui.html it is mentioned that,

Note: When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped (not destroyed). If the user navigates back to restore the fragment, it restarts. If you do not add the transaction to the back stack, then the fragment is destroyed when removed or replaced.

Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12
  • Hi, I am not using a `ViewPager`, as I am trying to get to the bottom of understanding how the Activity stores its fragments' states. Could you perhaps tell me how to achieve this with just a plain `Activity` and 1 single `Fragment`? Also, when minimizing, the `Fragment`'s `onSaveInstanceState()` is called, but doesn't seem to do anything :/ – logan May 28 '14 at 08:47