Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: onSaveInstanceState not being called from activity

I have an Activity A which calls Activity B. In Activity B, when i click on a button, finish() is called, which in turn calls onDestroy() of Activity B and returns to activity A.

According to android documentation, Before onDestroy is called, onSaveInstanceState(Bundle bundle) will be called, where i do the following.

@Override     public void onSaveInstanceState(Bundle outState) {          super.onSaveInstanceState(outState);         System.out.println("Saving webview state");         Log.d(TAG, "In onsave");         wv.saveState(outState);      } 

and the next time Activity B is started from Activity A,

in the oncreate(), i do the following:

onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);  if(savedInstanceState != null){ //restore webview }else { // code } } 

However, before calling onDestroy in Activity B, The onSaveInstanceState method is never called. any help on this will be greatly appreciated.

EDIT: if this is not possible. Please let me know if there is a way to store webview state

like image 805
Bharath Avatar asked Oct 09 '12 04:10

Bharath


People also ask

Is onSaveInstanceState always called?

I'm aware that onSaveInstanceState() is not always called when an activity is about to be destroyed. For example, if it is destroyed because the user has pressed the "back" button, the activity state is not preserved.

What is the purpose of using onSaveInstanceState () here?

Basically, onSaveInstanceState is used for the scenario where Android kills off your activity to reclaim memory. In that scenario, the OS will keep a record of your activity's presence, in case the user returns to it, and it will then pass the Bundle from onSaveInstanceState to your onCreate method.

What is use of onSaveInstanceState in Android?

onSaveInstanceState() is a method used to store data before pausing the activity.

What is onSaveInstanceState Kotlin?

Answer: onSaveInstanceState() and other such lifecycle callbacks give you an opportunity to save any application state that you want to restore when the user returns to your application. For example, let's say you have a simple text field in your Android App.


2 Answers

I had a similar situation. It was clearly a dev bug. I've overridden the wrong method:

public void onSaveInstanceState(Bundle outState,                                  PersistableBundle outPersistentState) 

Instead a correct one:

protected void onSaveInstanceState(Bundle outState) 
like image 112
Paweł Szczur Avatar answered Sep 28 '22 13:09

Paweł Szczur


Please notice that onRestoreInstanceState() is called when activity is recreated but only if:

it was killed by the OS. "Such situation happen when:

  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called."

So if you are in your activity and you hit Back button on the device, your activity is finish()ed and next time you start your app it is started again (it sounds like re-created, isn't it?) but this time without saved state because you intentionally exited it when you hit Back button.

like image 23
Marcin S. Avatar answered Sep 28 '22 14:09

Marcin S.