Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when my app has been killed?

I need to know when the user kills my app (Force stop). I've been reading the android lifecycle, which has the onStop() and onDestroy() functions, these are related to each activity the user ends on my app, but not when the user forces stop or kills my app.

Is there any way to know when the user kills the app?

like image 899
Darknoe Avatar asked Jan 10 '14 09:01

Darknoe


People also ask

How do you know if the app is killed?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

When app is killed IOS?

It varies per device, and is tighter on older devices. Prior to iPhone 6s, this limit is around 200 MB max, but after 6S, it grows higher. When this limit is exceeded, your app will be killed immediately. “jetsam” is what's known as a “Memory Pressure Exit”, and is the most common type of app termination.

How do I know if an app is running in the background Android?

You can detect currently foreground/background application with ActivityManager. getRunningAppProcesses() which returns a list of RunningAppProcessInfo records. To determine if your application is on the foreground check RunningAppProcessInfo.

How do you know app is killed in react native?

nextAppState === 'inactive' or checking if background does not tell you if the app is "Killed" (swiped out) or not. Because when the native layer is killed then the javascript engine is killed as well.


2 Answers

I have found one way to do this.....

  1. 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 like this

    <service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" /> 
  3. Then start this service on your splash activity

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

And now whenever you will clear your app from android recent Then this method onTaskRemoved() will execute.

NOTE: In Android O+ this solution only works when the app is full-time in foreground. After more than 1 minute with the app in background, the OnClearFromRecentService (and all other Services running) will be automatically force-killed by the system so the onTaskRemoved() will not be executed.

like image 73
Faisal Khan Avatar answered Sep 20 '22 13:09

Faisal Khan


there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled?

When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

When the user uninstalls the app, at first the process is killed, then your apk file and data directory are deleted, along with the records in Package Manager that tell other apps which intent filters you've registered for.

like image 41
Taranfx Avatar answered Sep 23 '22 13:09

Taranfx