How activity keep track of fragment after config. change
<p>I have couple of questions regarding fragment handling in Android.</p>

<p>In my activity, I initially create one fragment (<code>OnCreate</code>). Through the option menue, I swap out this fragment with <code>Fragment2</code>, <code>Fragment3</code>, etc. I DO NOT have <code>setRetainInstance</code> set to true in any of the fragment. Still if I rotate the device, it populate the correct (whichever was visible before the change) fragment. I would assume, Android would distory all the fragments associated with this activity, and would re-create based on</p>

<pre><code>if (ftl == null) {
                ftl = new Fragment1();
                ft.add(R.id.fragment_container, ftl, mTagFrag1);
            }
</code></pre>

<p>but, it finds the fragment.</p>

<p>so,</p>

<ol>
<li>Why the fragment is not killed.</li>
<li><p>How it knows which fragment was attached to the activity layout after the configuration change?</p>

<p>public class MyActivity extends BaseActivity {</p>

<pre><code>String mTagFrag1 = "Frag1";
String mTagFrag2 = "Frag2";
String mTagFrag3 = "Frag3";


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment ftl = fm.findFragmentByTag(mTagFrag1);

    if (ftl == null) {
        ftl = new Fragment1();
        ft.add(R.id.fragment_container, ftl, mTagFrag1);
    } else {
        // If it exists, simply attach it in order to show it
        // ft.attach(ftl);
    }
    ft.commit();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment f;
    switch (item.getItemId()) {
    case R.id.menu_fag1:
        f = fm.findFragmentByTag(mTagFrag1);
        if (f == null) {
            f = new Fragment1();
        }

        ft.replace(R.id.fragment_container, f);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        return true;
    case R.id.menu_fag2:
        // This menu item is from a fragment
        f = fm.findFragmentByTag(mTagFrag2);
        if (f == null) {
            f = new Fragment2();
        }

        ft.replace(R.id.fragment_container, f);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        return true;
    case R.id.menu_fag3:
        // This menu item is from a fragment
        f = fm.findFragmentByTag(mTagFrag3);
        if (f == null) {
            f = new Fragment3();
        }

        ft.replace(R.id.fragment_container, f);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
</code></pre></li>
</ol>
