Callback when application is killed by scrolling

I have an application that includes navigation. If the user starts navigation, a kind of "navigationLifecycleManager" is created. This is saved in the application instance so that it saves configuration changes, switches between acitivities, etc.

However, if the user "terminates" the application, I want to kill some background threads, store some minor data in the application store, and so on. So I need some kind of hook that will tell me when the application closes.

  • Navigation must withstand any activity life cycle (therefore, the application instance doesn’t care)
  • Navigation should be saved by pressing the home button.
  • Navigation should not end by tapping back.
  • Navigation should not be saved when it is killed, dragging it from the list of "recent applications".

You can definitely achieve this by overriding "onPause" and checking "isFinishing". But this does not solve the problem from the list of recent applications. Sweep seems to not cause anything. Neither "onPause", nor "onDestroy", nor "onTerminate" are called in the operation / application.

+6
source share
2 answers

You cannot process wipes because the system simply deletes your process from memory without calling a callback.

I checked that before the user calls the screen "recent applications", onPause() will always be called. Therefore, you need to save all the data in the onPause method without checking isFinishing() .

To check the back button, use the onBackPressed method.

+3
source

This may be a call to the service.

Here's what you can do if you just stop the service when the application is killed by scrolling from the list of recent applications.

Inside the manifest file, save the stopWithTask flag as true for the service. How:

 <service android:name="com.myapp.MyService" android:stopWithTask="true" /> 

But, as you say, you want to unregister the listeners and terminate the notification, etc., I would suggest this approach:

  • Inside the manifest file, save the stopWithTask flag as false for the service. How:

     <service android:name="com.myapp.MyService" android:stopWithTask="false" /> 
  • Now in your MyService service MyService override the onTaskRemoved method. (This will only start if stopWithTask set to false ).

     public void onTaskRemoved(Intent rootIntent) { //unregister listeners //do any other cleanup if required //stop service stopSelf(); } 

See this question for more details, which also contains another piece of code.

  1. Start the service as shown below.

startService (new Intent (this, MyService.class));

Hope this helps.

Original source see here

+1
source

All Articles