Android recovery in activity is how to do

A has an active application, user-initiated activities on the basis of A on the activities of B, A activity entered the stopped state, this time due to lack of system memory, the active A recovery swap, then press the back button to return to the user A activity will What happens? A normal display but still does not perform Restart () method but onCreate execution of activity A () method, in which case A activity will be re-created again. So the question is: if there may be a temporary data and state A, such as: A Chinese this box, you enter some text, and then start memory B and A are withdrawn, click back back because of A is A re-create the text box, everything is gone, it will affect the user experience. Solution: Use onSaveInstanceState () callback method, this method can guarantee will be called before the activity is recovered, so you can use this method to solve when the activity is not recovered temporarily stored data problems. Add onSaveInstanceState in MainActivity in () method to temporarily save the Soviet Union drama protected void onSaveInstanceState (Bundle outState) {super.onSaveInstanceState (outState); String tempData = "hello how are you?"; OutState.putString ( "data_key", tempData) ;} in onCreate () method also has a parameter Bundle; 9-63 + so that we can write protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); if (savedInstanceState! = null) {String tempData = savedInstanceState.getString ( "data_key");}}

Guess you like

Origin www.cnblogs.com/hy-nobug/p/12129465.html
Recommended