CoDe
CoDe

Reputation: 11146

Fragment.onCreateView called on configuration change?

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.

Upvotes: 1

Views: 1848

Answers (2)

matiash
matiash

Reputation: 55350

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?

Upvotes: 1

marcinj
marcinj

Reputation: 49986

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.

Upvotes: 1

Related Questions