Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - saving application state

I want to save application state to be able to restore it after another launch. Is is it better to use method onSaveInstanceState and save it to Bundle or to use SharedPreferences?

Thanks

like image 266
Waypoint Avatar asked Oct 13 '11 07:10

Waypoint


People also ask

What is saving state in Android?

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.

Which of the following is are appropriate for saving the state of an Android application?

onPause() is the appropriate for saving the state of an Android application.

How can you save the state of activity?

When the activity goes into the background, the system calls onSaveInstanceState() . You should save the search query in the onSaveInstanceState() bundle. This small amount of data is easy to save. It's also all the information you need to get the activity back into its current state.

What are the different states in Android app life cycle?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .


1 Answers

It depends on your intention. Using the onSaveInstanceState() is only a reasonable solution if you want to ensure saving the state during configurations changes and other restarting events. In case you aim for a true saving of the application's state beyond the lifecycle of the application, you should consider using either the SharedPreferences or maybe even employ a database.

like image 145
Till Helge Avatar answered Sep 30 '22 17:09

Till Helge