10

What is the significance of:

super.onCreate(null);

instead of

super.onCreate(savedInstanceState);

With this change, I am able to avoid many problems that otherwise plague my Activitys each time a configuration change occurs (rotation, locale shift, permission toggle). It seems that with this change, the Activity is started afresh whenever a configuration change triggers it to restart. And I don't seem to lose any data or process state by doing this: all my Activitys are restored exactly to their former state.

My question is, can I do this with impunity henceforth, or am losing something in the bargain? I don't really understand why this works, whether it is safe or not, and what unintended effects it may have on my app.

I chanced upon this trick here.

Related Questions:

Calling super.onCreate() with null parameter?

Will 'Bundle savedInstanceState' be alive after Application is being killed?

Activity state instance - insights?

Activity's instance state: what is automatically stored and restored

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • See this thread: http://stackoverflow.com/questions/15115975/calling-super-oncreate-with-null-parameter – StefanoM5 Sep 09 '16 at 10:55
  • 1
    `all my activities are restored exactly to their former state` that's really weird. You should lose all of the previous state if you perform `super.onCreate(null)`. Are you sure, that you do not specify `configChanges` flag in `AndroidManifest.xml`? – azizbekian Dec 07 '18 at 11:07
  • are you using fragments? Are the issues you're facing to do with the linked [trick](https://coderanch.com/t/602443/Dynamic-Fragments-access-recreated-saved) –  Dec 09 '18 at 09:54
  • it will work inside your activity, but you have any fragment which contains any data inside onSaveInstanceState(), it will not be restored. – Jitesh Mohite Dec 10 '18 at 03:06
  • @azizbekian: I have specified `android:configChanges="orientation|keyboardHidden|locale"` for all my Activities. – Yash Sampat Dec 10 '18 at 07:08
  • @YvetteColomb: the Activities where I have done this do not contain Fragments. The ones that do have `super.onCreate(savedInstanceState)`. I believe this is related to when an app is *resumed* after toggling a runtime permission in Settings (which leads to the last visited Activity being recreated and the app process being killed & restarted). – Yash Sampat Dec 10 '18 at 07:12
  • 1
    @Y.S. Can you elaborate on _...I am able to avoid many problems that otherwise plague my Activitys each time a configuration change occurs_? [Calling super.onCreate() ...](https://stackoverflow.com/a/15116477/2558882) does answer your question. You're basically breaking the contract, and even though you can live with the side-effects (right now), you can't be sure if this will break in any manner. I'm almost certain that you won't ever see a crash (`null` being an acceptable value) doing this. I guess it's a trade-off situation, unless you've misidentified the root cause of your _problems_. – Vikram Dec 14 '18 at 02:59
  • 1
    @Y.S. I _don't_ recommend doing this. But, if you have to, go though the source code to identify any possible side-effects. [AppCompatDelegateImpl](http://androidxref.com/9.0.0_r3/xref/frameworks/support/v7/appcompat/src/main/java/androidx/appcompat/app/AppCompatDelegateImpl.java#279) [Activity](http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/app/Activity.java#1034) Would appreciate if you could _edit_ your question with the problems you've encountered by passing _state_ as is. – Vikram Dec 14 '18 at 03:07
  • @Vikram: This is related to certain unwanted effects after an Activity restarting due to a runtime permission toggle (from Settings). I needed the Activity to restart as a perfectly brand new instance. – Yash Sampat Dec 14 '18 at 06:53

5 Answers5

2

onCreate() call first when activity is about to create, Also Android System manage activity lifecycle and can kill the activity with saving its instanceState, in case if acitvity out of focus for user for long time and system is on low memory situation.

An activity has essentially four states

super.onCreate(null) : Would always create activity as it is creating fisrt time , even Android system provide its savedInstanceState, and does't matter what orientation configurations are.

super.onCreate(savedInstanceState) : Activity can use 'savedInstanceState' to reset its state or component where it was last.

To achive this, acitivty's instance state need to be persist before activity lost user attention( that could be onStop or onDestroy)

savedInstaceState can also be important to handle if activity configuration got changed, Please check acitvity life cycle behavior on Configuration change

Anand Tiwari
  • 1,583
  • 10
  • 22
1

am losing something in the bargain?

Only if you are working with Fragments. see Calling super.onCreate() with null parameter?

Yes, onCreate(...) is necessary to start an Activity, but passing Bundle as an argument is required when you are working with fragments.

Read this

What did you infer from that?

The argument savedInstanceState is anyway null by default. So you aren't really losing anything in a bargain.

But wait, we usually use Bundles to maintain orientation change, right?

the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.

the following onConfigurationChanged() implementation checks the current device orientation:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

But Remember: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged().

Daksh Gargas
  • 3,498
  • 2
  • 23
  • 37
0

As far as I know a lot of data is saved in the bundle savedInstanceState. E.g. all the views' states in your current layout, such as the current content of any EditText or CheckBox.

You could also look up some official sources to check whether you need to keep some data.

Here's a nice article about it

Basically it says that all View class implements the methods onRestoreInstanceState and onSaveInstanceState, which are saving and restoring any temporary states they were in before the state change.

abbath
  • 2,444
  • 4
  • 28
  • 40
-1

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

It is very important to use savedInstantState to get values from Intent which is saved in the bundle.

-1

All your data stored in class variables or local variables is lost whenever you change rotation of device, but in your activity it looks like you have not stored any data as long as user enters any data, but instead you are perhaps reading data on click of a button or something like that, your activity will behave and work normally, and all user inputs like text inside EditText will be restored by Android itself, because it identifies "IDs" (android:id="@+id/anyID") allotted to each view and can restore by itself all the values inserted by user.

I hope this this helps you...

Happy coding :)

Abdul Aziz
  • 442
  • 5
  • 12