Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I restore savedinstancestate in onCreate or in onRestoreInstanceState?

I have an activity that starts some other activities for results, so when the result comes back, the activity may or may not have been destroyed and recreated.

I have overridden onSaveInstanceState so as to add the data that needs to be preserved and restored.

When the activity gets destroyed and recreated, onCreate is passed the savedInstanceState bundle; but also onRestoreInstanceState() is called and passed the same bundle.

So where should I put the code that extracts the data from the bundle and restores the state? In onCreate or in onRestoreInstanceState? Is the latter guaranteed to be always called?

Is it possible that onRestoreInstanceState is called without calling onCreate? (e.g. if the activity gets stopped and restarted but not destroyed and recreated)?

like image 863
matteo Avatar asked May 05 '14 12:05

matteo


People also ask

What is the purpose of super onCreate savedInstanceState ); method?

By calling super. onCreate(savedInstanceState); , you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.

What is onCreate bundle savedInstanceState?

What is the savedInstanceState Bundle? 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.

What is the significance of onSaveInstanceState () and onRestoreInstanceState ()?

The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.

What is onCreate () meant for?

onCreate(Bundle savedInstanceState) Function in Android:When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to create the activity.


1 Answers

"Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is NULL"

following link explain pretty clearly about restart activity.

Android Guide

like image 85
mcd Avatar answered Oct 10 '22 22:10

mcd