Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onPause with Android?

Tags:

android

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:
Emulator

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.

like image 528
kimo Avatar asked Feb 05 '11 16:02

kimo


People also ask

How do I use onPause on Android?

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.

When onPause () method is called in Android?

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.

What is the difference between onPause and onStop Android?

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.

What is the use of an onStop () function in Android?

The onStop() function is called when the application enters the stopped state. In this state, the activity is no longer visible to the user.


1 Answers

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.

like image 138
dave.c Avatar answered Oct 17 '22 14:10

dave.c