Android View - What is automatic saved and restored in Activity -


i beginner android.

in android, generic elements can automatically saved/restored in onsaveinstancestate/onrestoreinstancestate.

for example, edittext save/restore text property, ratingbar save/restore rating property...

i see test can't find nothing in documentation.

my question :

how can know saved/restored without intervention?

for example, can find edittext.text automatically saved/restored?

i precise don't want test properties.

edit jrg answer :

https://developer.android.com/guide/components/activities/activity-lifecycle.html

save activity state activity begins stop, system calls onsaveinstancestate() method<...> default implementation of method saves transient information state of activity's view hierarchy, such text in edittext widget or scroll position of listview widget.

how can know default implementation of save/restore?

second edit after reread jrg answer :

by default, system uses bundle instance state save information >each view object in activity layout (such text value entered >an edittext widget).

the default implementation save/restore state of element view.

android documentation explains saving states , article on saving states in activity , fragment.

saving , restoring activity state there few scenarios in activity destroyed due normal app behavior, such when user presses button or activity signals own destruction calling finish() method. system may destroy process containing activity recover memory if activity in stopped state , hasn't been used in long time, or if foreground activity requires more resources.

when activity destroyed because user presses or activity finishes itself, system's concept of activity instance gone forever because behavior indicates activity no longer needed. however, if system destroys activity due system constraints (rather normal app behavior), although actual activity instance gone, system remembers existed such if user navigates it, system creates new instance of activity using set of saved data describes state of activity when destroyed. saved data system uses restore previous state called instance state , collection of key-value pairs stored in bundle object.

by default, system uses bundle instance state save information each view object in activity layout (such text value entered edittext widget). so, if activity instance destroyed , recreated, state of layout restored previous state no code required you. however, activity might have more state information you'd restore, such member variables track user's progress in activity.

save activity state activity begins stop, system calls onsaveinstancestate() method activity can save state information collection of key-value pairs. default implementation of method saves transient information state of activity's view hierarchy, such text in edittext widget or scroll position of listview widget. app should implement onsaveinstancestate() callback after onpause() method, , before onstop(). not implement callback in onpause().

caution: must call superclass implementation of onsaveinstancestate() default implementation can save state of view hierarchy.

to save additional state information activity, must override onsaveinstancestate() , add key-value pairs bundle object saved in event activity destroyed unexpectedly. example:

static final string state_score = "playerscore"; static final string state_level = "playerlevel"; ...   @override public void onsaveinstancestate(bundle savedinstancestate) {     // save user's current game state     savedinstancestate.putint(state_score, mcurrentscore);     savedinstancestate.putint(state_level, mcurrentlevel);       // call superclass can save view hierarchy state     super.onsaveinstancestate(savedinstancestate); } 

note: in order android system restore state of views in activity, each view must have unique id, supplied android:id attribute.

to save persistent data, such user preferences or data database, should take appropriate opportunities when activity in foreground. if no such opportunity arises, should save such data during onstop() method.

restore activity state when activity recreated after destroyed, can recover saved state bundle system passes activity. both oncreate() , onrestoreinstancestate() callback methods receive same bundle contains instance state information.

because oncreate() method called whether system creating new instance of activity or recreating previous one, must check whether state bundle null before attempt read it. if null, system creating new instance of activity, instead of restoring previous 1 destroyed.

for example, following code snippet shows how can restore state data in oncreate():

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate); // call superclass first       // check whether we're recreating destroyed instance     if (savedinstancestate != null) {         // restore value of members saved state         mcurrentscore = savedinstancestate.getint(state_score);         mcurrentlevel = savedinstancestate.getint(state_level);     } else {         // initialize members default values new instance     }     ... } 

instead of restoring state during oncreate() may choose implement onrestoreinstancestate(), system calls after onstart() method. system calls onrestoreinstancestate() if there saved state restore, not need check whether bundle null:

public void onrestoreinstancestate(bundle savedinstancestate) {     // call superclass can restore view hierarchy     super.onrestoreinstancestate(savedinstancestate);       // restore state members saved instance     mcurrentscore = savedinstancestate.getint(state_score);     mcurrentlevel = savedinstancestate.getint(state_level); } 

caution: call superclass implementation of onrestoreinstancestate() default implementation can restore state of view hierarchy.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -