4

I see on Android document that :

public void setRetainInstance (boolean retain)

Since: API Level 11 Control whether a fragment instance is retained across Activity re-creation (such as from a >configuration change). This can only be used with fragments not in the back stack. If set, >the fragment lifecycle will be slightly different when an activity is recreated:

Could anyone explain me more about "This can only be used with fragments not in the back stack". I build a simple example to test. I put the fragment to back stack. But setRetainInstance method still have effect. I put fragment to backstack like this :

    FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
    
    MyFragment myFragment = new MyFragment();
    myFragment.setHasOptionsMenu(true);
    transaction2.replace(R.id.fragment_container, myFragment);
    transaction2.addToBackStack(null);
    transaction2.commit();

And in my fragment oncreate method :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);

    Log.v("MyFragment", "onCreate");
}

I am newbie in Fragment. Please help me figure out this problem. Note : I used : SherlockFragment

Community
  • 1
  • 1
gZerone
  • 673
  • 12
  • 28

0 Answers0