How to process code when application is killed by scrolling in Android?

If my application is running and I press the home button, the application leaves in the background. Now, if you press the home button for a long time and kill the application by pulling it from the list of recent applications, none of the events like onPause() , onStop() or onDestroy() is called, and the process stops. Therefore, if I want my services to stop, kill notifications and unregister listeners, how can I do this? I read a lot of articles and blogs, but did not receive any useful information, and I did not find any documentation about this. Any help would be greatly appreciated. Thank you in advance.

+91
android
Oct 24 '13 at 14:08
source share
8 answers

I just solved a problem of this type.

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(); } 

Refer to my question for more details, which contains another piece of code.

Hope this helps.

+136
Nov 12 '14 at 8:38
source share

Found one way to do it

make one service like this

 public class OnClearFromRecentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("ClearFromRecentService", "Service Started"); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d("ClearFromRecentService", "Service Destroyed"); } @Override public void onTaskRemoved(Intent rootIntent) { Log.e("ClearFromRecentService", "END"); //Code here stopSelf(); } 

}

2) register this service in manifest.xml

 <service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" /> 

3) Then run this service on your pop-up activity

 startService(new Intent(getBaseContext(), OnClearFromRecentService.class)); 

And now whenever you clear your application of recent Android, then this onTaskRemoved () method will execute.

+30
Mar 17 '17 at 11:49
source share

I solved a similar problem. If you want, after scrolling from a recent task, and the next time it starts, it will behave correctly, follow these steps: -

1) Save process ID in general preferences:

SharedPreferencesUtils.getInstance().putInt(SharedPreferencesUtils.APP_PROCESS_ID, android.os.Process.myPid());

2) When the application starts from the launcher after clearing a recent task, do the following:

 int previousProcessID = mSharedPreferencesUtils.getInt(SharedPreferencesUtils.APP_PROCESS_ID); int currentProcessID = android.os.Process.myPid(); if ((previousProcessID == currentProcessID)) { // This ensures application not killed yet either by clearing recent or anyway } else { // This ensures application killed either by clearing recent or by anyother means } 
+14
Oct 07 '15 at 13:34 on
source share

When you press home - onPause and onStop your activity is called, so at this time you should do all the savings and cleanup, because the Android platform does not guarantee that onDestroy or any other life cycle method will be involved, so the process can be killed without notice.

+5
Oct 24 '13 at 14:16
source share

You need to save your data when onPause() called. Take a look at this lifecycle diagram: Android Developer

You can see that the application can be killed after onPause() or onStop() .

Manage your data and restore it to onRestart() \ onCreate() .

Good luck

+1
Oct 24 '13 at 14:33
source share

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

I checked that before the user calls up the "recent applications" screen, 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.

+1
Jul 28 '17 at 10:46
source share

This worked for me on Android 6,7,8,9.

Make one service like this:

  public class OnClearFromRecentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("ClearFromRecentService", "Service Started"); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d("ClearFromRecentService", "Service Destroyed"); } @Override public void onTaskRemoved(Intent rootIntent) { Log.e("ClearFromRecentService", "END"); //Code here stopSelf(); } } 

2) Register this service in manifest.xml :

 <service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" /> 

3) Then start this service in its popup activity

  startService(new Intent(getBaseContext(), OnClearFromRecentService.class)); 
0
Aug 26 '19 at 23:52
source share

ViewModel.onCleared () can be useful if the goal is to free up some resource (perhaps a system running somewhere else on the network) when the user performs an unexpected exit by swiping his finger and not pressing the Stop button or button. [That is how I came to this question].

The application does not receive notifications, and Activity.onDestroy () is called for configuration changes, such as orientation changes, so there is no answer. But ViewModel.onCleared is called when the application is uninstalled (and also when the user exits the action). If the resource you want to use is associated with more than one action on the stack, you can add link counts or some other mechanism to decide whether ViewModel.onClear should free the resource.

This is another of many good reasons to use ViewModel.

Bean

0
Sep 10 '19 at 15:48
source share



All Articles