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
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.
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.
onSaveInstanceState() is a method used to store data before pausing the activity.
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.
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)
Please notice that onRestoreInstanceState()
is called when activity is recreated but only if:
it was killed by the OS. "Such situation happen when:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With