Java – How do I call a method when an Android application closes or loses focus?

How do I call a method when an Android application closes or loses focus?… here is a solution to the problem.

How do I call a method when an Android application closes or loses focus?

Because I’m building an application that will handle fairly sensitive data, I want to synchronize the SQLite database with the server every time the user logs in and delete the database every time the application loses focus (as the user moves to the home screen or another application).

See the activity lifecycle The idea is to do this by emptying the database in the onDestroy of each activity. To test the described lifecycle, I just cover all the lifecycle methods (onCreate, onStart, onResume, onPause, onStop, and onDestroy), include some log messages, and launch my application.

Log messages are included in my SettingsActivity. When I go into my app and move to settings, it runs onCreate, onStart, and onResume (as expected). Then, when I click on a setting and move to the next screen, it runs onPause and onStop (still as expected). To go back to the settings screen, I click the back button and it runs onStart and onResume again (still as expected), and when I now click the back button again to return to the splash screen, it (to my surprise) runs onPause, onStop and onDestroy.

So my question;

  1. Why destroy an Activity when the application is not complete?
  2. More importantly: how do I run my CleanUp method when the app closes or loses focus?

Solution

You can get more information here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html

Even though I think you’ve read it because you’ve studied the activity lifecycle, you can see in the first diagram that onDestroy() is called after onStop() and this call is completely managed by the system: you shouldn’t expect any behavior. The system will decide when to call this method at its discretion, and sometimes, this method will never be called (see here: ). http://developer.android.com/reference/android/app/Activity.html)。 When the system needs memory, your activity passes in onStop() and that’s it.

So, to answer your second question, you should read the description of onDestroy() in the documentation. Method:

Note: do not count on this method being called as a place for saving
data!
For example, if an activity is editing data in a content
provider, those edits should be committed in either onPause() or
onSaveInstanceState(Bundle), not here. This method is usually
implemented to free resources like threads
that are associated with an
activity, so that a destroyed activity does not leave such things
around while the rest of its application is still running. There are
situations where the system will simply kill the activity’s hosting
process without calling this method (or any others) in it, so it
should not be used to do things that are intended to remain around
after the process goes away.

So obviously, this is not a good place to carry out the cleanup process. So you should use one of the onPause() or onStop() methods.

onStop() is described in the documentation as follows:

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

onPause() is described in the documentation as follows:

Called as part of the activity lifecycle when an activity is going
into the background, but has not (yet) been killed.
[…]
When activity B is launched in front of activity A, this callback will be
invoked on A. B will not be created until A’s onPause() returns, so be
sure to not do anything lengthy here.

[…]
In situations where the system needs more memory it may kill paused processes to reclaim resources.

We now know that onPause() is designed to allow you to save data, and that after onPause() data has been executed, the system may terminate your process. So cleaning up in onPause() seems like the safest place to do because you’re pretty sure you’ll call it every time.

Also, as you can see, cleaning up here slows down your app, but cleaning and recreating the database every time it gains/loses focus anyway is a very heavy process….

To recover: clean up the process in onPause() and your initialization process. Keep in mind that your application can be very slow using this process.

Hope this helps.

Related Problems and Solutions