using onSaveInstanceState(Bundle ..) and onRestoreInstanceState(Bundle ..)
was really good and work, but it is working when click Turn off button on Emulator.
Now, i want to save state and restore the saved data when below button used:
I think it is possible to use OnPause() OR oOnStop(), if i am right, How to use it,
it will be enough to show me Java source of saving one boolean variable, and restore it,
Thanks.
you can just override onPause() in your activity say activtyA when you are navigating to another activity say activityB and override onResume() when u come back to the activityA.
There are two lifecycle methods that deal with when the activity is paused and when it becomes active again: onPause() and onResume() . onPause() gets called when your activity is visible but another activity has the focus.
If screen times out on your activity, then onPause is called. After sometime if you will not open the screen then onStop will be called.
The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user.
I would use onPause()
, as onStop()
is not guaranteed to be called. See the application fundamentals for details on the lifecycle.
To save and restore a boolean, I would use SharedPreferences
. There is a code example on the data storage page that shows how to save and restore a boolean. They use onCreate()
and onStop()
, but I would use onResume()
and onPause()
, for the reasons I have already mentioned.
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