Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity instance state

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

like image 394
Y.S Avatar asked Sep 09 '16 10:09

Y.S


1 Answers

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

like image 175
Anand Tiwari Avatar answered Sep 26 '22 03:09

Anand Tiwari