1

Here I wrote an simple activity and fragment. I changed orientation and since it's configuration change call so it called Activity.onCreate() again but why Fragment.onCreateView() again. Since I am not calling fragment child on configuration change call.

Here is my code :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_container);

        log("life_cycle_activity", "onCreate");

        if(savedInstanceState != null) {

            log("life_cycle_activity", "ohh..configuration changed");
            return;
        } else {

            launchChildFragment();
        }
    }

NOTE: android:configChanges not work on Settings->Display->FontStyle change

Any suggestion how can prevent Fragment call on configuration change.

CoDe
  • 11,056
  • 14
  • 90
  • 197

2 Answers2

1

You can call:

public void setRetainInstance (boolean retain)

with true value in your fragment onCreate - this will create the so called retained fragment,

or prevent activity config changes using:

android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|locale|fontScale|mnc|mcc"

in manifest.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Hi, Thanks.but still Fragment method getting called..and regarding config para in manifiest..I am trying with settign->display->font style ..and I found nothing for it to handle in configChange. Your opinion please. – CoDe Dec 30 '14 at 12:51
  • @Shubh you can also check if configchange is in progress: isChangingConfigurations() - since api11, look also into docs: http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean), it clearly states: `Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). `,also look into this detailed SO: http://stackoverflow.com/questions/11182180/understanding-fragments-setretaininstanceboolean – marcinj Dec 30 '14 at 13:08
1

Fragments are being recreated because the savedInstanceState parameter includes information on the "previous" Fragments present in the Activity. Check the implementation for the onRetainNonConfigurationInstance() method (and its corresponding logic in onCreate()) in FragmentActivity and you'll see where this information is stored and reloaded.

As for the "font size" configuration change, it doesn't seem to be possible (or at least I didn't find a way) to handle it with android:configChanges. See this question.

As an aside though:

Any suggestion how can prevent call for Fragment.onViewCreate on configuration change.

Why do you need to do this?

Community
  • 1
  • 1
matiash
  • 54,791
  • 16
  • 125
  • 154
  • Thanks @Matiash for info. I also found same analysis. Regards Fragment.onViewCreate prevention...I just wanted to avoid recreation call..But now I am pretty much clear about process. – CoDe Jan 02 '15 at 09:24