8

I have a doubt on configuration change handling by os, Lets say I have an Activity inside its onCreate() it is creating one (or more) fragment instance using a special constructor defined in that fragment.

When device orientation changes system will destroy and create the fragment again, if I am correct it will use default (no argument) constructor to do that, meanwhile the activity is also getting recreated and it will again instantiate fragment using same constructor. My question is, will there be two different instances in the memory? if not, how they are resolved and become one?

Punit Sharma
  • 2,951
  • 1
  • 20
  • 35
duckduckgo
  • 1,280
  • 1
  • 18
  • 32
  • 1
    it is like(simplified version of Fragment and Activity lifecycle for this case)OldFragmenInstance.onSaveInstanceState() => Destroy fragment and Activity => new Activity => Activity.OnCreate() => newFragmenInstance = new Fragment() => newFragmenInstance.setArguments() => newFragmenInstance.onCreate(savedDAta_sameAsInonSaveInstanceState) => Activity's fragment manager reattach the fragment ... onSaveInstanceState save most common UI controls state(if fragment has such) by default ... **using Fragment constructor with paramteres makes no sens => use set/getArguments instead** – Selvin Jan 18 '16 at 13:09
  • @selvin but when i print (log) the hashcode of instances created inside Activity's onCreate and in fragment's onCreateView() they are same when app is launched first but when i reorient device it will give two different values in those places. – duckduckgo Jan 18 '16 at 13:24
  • Since i am also using ViewPager a related discussion is here http://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state – duckduckgo Jan 19 '16 at 04:03

1 Answers1

5

Some background

The responsibility to keep the state of a fragment throughout the life of the activity is on the FragmentManager, this is why there is an option to commit and to commitAllowingStateLoss when committing a fragment transaction. If left to it's own devices the Fragment's state will be restored automatically. But... if you add the fragment in code (as opposed to adding it in the xml layout) then it is up to you to add it only when needed.
Usually, in the case of onCreate it is enough to check that the activity is not being restarted, that is, check that savedInstanceState == null and only then add the fragment.

Take a look at this snippet from the fragment's guide:

public static class DetailsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getResources().getConfiguration().orientation
            == Configuration.ORIENTATION_LANDSCAPE) {
        // If the screen is now in landscape mode, we can show the
        // dialog in-line with the list so we don't need this activity.
        finish();
        return;
    }

    if (savedInstanceState == null) {
        // During initial setup, plug in the details fragment.
        DetailsFragment details = new DetailsFragment();
        details.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
    }
}

}

P.S.

The answer to your question:

will there be two different instances in the memory?

Yes, if you just add the fragment on each call to onCreate there will be more than one instance.

Alex.F
  • 5,648
  • 3
  • 39
  • 63