4

I want to perform some operation when my application gets killed. Which method can be used for this? I am working on Android 5.0.

Floern
  • 33,559
  • 24
  • 104
  • 119

3 Answers3

6

The key of this question is that:

you must understand your application whether can receive any additional callbacks when your application being killed in any kinds of situation.

The following answer is answered by Devunwired in this question:

Android app doens't call "onDestroy()" when killed (ICS)

This will help you more to understand this.


Your application will not receive any additional callbacks if the process it terminated by external means (i.e. killed for memory reasons or the user Force Stops the application). You will have to make do with the callbacks you received when you app went into the background for your application cleanup.

finish() is only called by the system when the user presses the BACK button from your Activity, although it is often called directly by applications to leave an Activity and return to the previous one. This is not technically a lifecycle callback.

onDestroy() only gets called on an Activity as a result of a call to finish(), so mainly only when the user hits the BACK button. When the user hits the HOME button, the foreground Activity only goes through onPause() and onStop().

This means that Android doesn't provide much feedback to an Activity to differentiate a user going Home versus moving to another Activity (from your app or any other); the Activity itself simply knows it's no longer in the foreground. An Android application is more a loose collection of Activities than it is a tightly integrated singular concept (like you may be used to on other platforms) so there are no real system callbacks to know when your application as a whole has been brought forward or moved backward.

Ultimately, I would urge you to reconsider your application architecture if it relies on the knowledge of whether ANY Activity in your application is in the foreground, but depending on your needs, there may be other ways more friendly to the framework to accomplish this. One option is to implement a bound Service inside of your application that every Activity binds to while active (i.e. between onStart() and onStop()). What this provides you is the ability to leverage the fact that a bound Service only lives as long as clients are bound to it, so you can monitor the onCreate() and onDestroy() methods of the Service to know when the current foreground task is not part of your application.

You might also find this article written by Dianne Hackborn to be interesting covering in more detail the Android architecture and how Google thinks it ought to be used.

Community
  • 1
  • 1
ifeegoo
  • 7,054
  • 3
  • 33
  • 38
1

You have to use Service Class for it like -

public class Myservice extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; }

@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(Constants.TAG, "Service Started"); return START_NOT_STICKY; }

@Override public void onDestroy() { super.onDestroy(); Log.d(Constants.TAG, "Service Destroyed"); }

@Override public void onTaskRemoved(Intent rootIntent) { Log.e(Constants.TAG, "END"); //Perfome here want you want to do when app gets kill stopSelf(); } }

In Manifest -

<service android:name="Myservice"
            android:stopWithTask="false" />

In Oncreate of your launcher activity or Application Class to start service -

startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
sanil
  • 482
  • 1
  • 7
  • 23
0

You can use your activity's onDestroy() method.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tony
  • 29
  • 8
  • 1
    Tried using onDestroy() activity but it doesn't works , I have to capture application getting kill event ... –  Oct 26 '15 at 02:13
  • Then you should try making the separate Service application, which would capture this event. – Tony Oct 26 '15 at 11:51
  • Thanks ! This issue got resolved by creating a shell script which will run in background and will monitor the application process status,Once it find process is missing it will invoke the task which need to be completed. –  Oct 26 '15 at 15:28