When to save data in a database, onPause () or onStop ()?

I know that this question was asked a million times, I myself, although I already knew the answer and that it was correct that the only guaranteed call is onPause (), so you should save your data there.

However, in many places in the Android documentation, they always suggest not to do the hard work (for example, writing data to the database) in the onPause () method, since this delays the transition between actions.

According to Android Developer's Guide in table 1

onPause (): this method is usually used to transfer unsaved changes to persistent data, stop animations and other things that the processor may consume, and so on. He must do everything that he does very quickly, because the next action will not be resumed until it returns.

Killable: YES

Then, according to the Android Developer's Reference Guide in a similar table .

He says the same thing, but:

Killable: Pre-HONEYCOMB

And they add a little note that says:

Keep in mind that this semantics will change slightly between platform-oriented applications, starting with HONEYCOMB and from targeting to previous platforms. Starting with Honeycomb, the application is not in the killable state until onStop () returns it . This affects when onSaveInstanceState (Bundle) can be called (it can be safely called after onPause () and allows the application to wait safely while onStop () remains constant.


Killable

Pay attention to the "Killable" column in the above table - for those methods that are marked as to be destroyed, after this method returns the process in which this activity is performed, it can be killed by the system at any time without another line of code being executed.

FOR POST-HONEYCOMB (I don't care about earlier versions): So, can we assume that any Android device (including different ROMS) will provide an onStop call regarding activity? And this is the best place where you can spend a lot of time storing the application?

Note. This is very confusing, since most of the answers here, sites, books, and even online tests for Android accept as the correct answer that you should save it in onPause and NOT on onStop.

+8
android android-activity android-lifecycle android-database
source share
2 answers

When to save data to a database, onPause () or onStop ()?

Or. They are almost identical, especially on Android 3.0+.

If the action that captures the foreground is a typical full-screen activity, so that the earlier activity is no longer visible, onPause() and onStop() will be called in quick succession.

If the action performing the foreground looks more like a dialog in which an earlier activity is still visible, onPause() will be called but not onStop() until the activity is a longer view (for example, now user presses HOME).

Most applications are not bothered by the "themed to like like dialog" script, in which case onPause() and onStop() are called one immediately after the next, and you can expand your background thread to save your data depending on what makes sense for you.

However, in many places in the Android documentation, they always suggest that you do not do heavy work (for example, writing data to the database) in the onPause () method, since this delays the transition between actions.

The same goes for onStop() , since both of these methods are called in the main thread of the application.

So, can we assume that any Android device (including different ROMS) will provide an onStop call regarding activity?

Both onPause() and onStop() will have the same characteristics in terms of process termination. Any of them must be called (a normal case), or none of them will be called (for example, you crash, the battery pushes the back of the phone).

And is this the best place to record time in the App Store app?

Either onPause() or onStop() are great places to start work done in the background thread to save your data. If you prefer to do this work in onStop() , you can do it. Personally, I'm the onPause() guy.

+13
source share
  • If you want more security, store it in onPause .
  • If your data is so large that you have to store it for several seconds, you can open the Service background (for example, IntentService ) for saving.
  • You can also check the system version in your code and choose when to save. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){}
  • In most situations, this rule of when to save will not be changed by some custom os. But, of course, there may be other OSs that certainly changed it. So, the most important thing in Android development is that you need to know that everything can be different in different phones.
0
source share

All Articles