Chen R
Chen R

Reputation: 51

android: how to cleanup application when it is killed

I'm working on an android application, my application turns on some HW on the phone. I want to close the HW in case anyone kills my application. how can i "catch" the kill command/event/signal and do a cleanup before application exit?


Some update what I found. You can't protect your application from being killed in all cases. onDestroy() is not guaranteed to be called (in case of OOM killer for example, or user calling killall -9 xxx). Best you can do is implement the onDestroy() function, add handler for sig_term and register your application as out of scope for the OOM killer.

This should handle 90% of the cases. All other cases are from specific user activity - so he will get what we wanted i guess...

Upvotes: 5

Views: 7761

Answers (4)

doppelhelix
doppelhelix

Reputation: 77

onPause() method comes before onStop() method in the activity life cycle. Your application may be killed by android when paused or when stopped, you may want to add isFinishing()method in the onPause to clean up. You wont have to do this in stop because pause will be initialized prior to stop anyway. onStop() is not guaranteed to execute if you are killed while paused.

@Override
protected void onPause() {
    super.onPause();
    //pause actions
    if (isFinishing()) {
        //checks to see if activity is finishing
        //type your code to execute clean up here
    }
}

see isFinishing() for more detail: developer.android.com/reference/android/app/Activity.html#isFinishing()

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006779

how can i "catch" the kill command/event/signal and do a cleanup before application exit?

You can't.

If the hardware is being used by an activity, release it in onPause().

If the hardware is being used by several activities, you will need to go through some gyrations with a reference count to arrange to release it when the last of those activities is paused.

If the hardware is being used by something else, you have problems.

Upvotes: 2

fadisdh
fadisdh

Reputation: 602

override onDestroy() on your main activity to execute the code when your app get destroyed, or onStop() to execute the code when the user exits your app.

onDestroy(): Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

onStop(): Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // The activity is being created.
}

@Override
protected void onStop() {
    super.onStop();
    // The activity is no longer visible (it is now "stopped")
}
@Override
protected void onDestroy() {
    super.onDestroy();
    // The activity is about to be destroyed.
} }

I highly recommend reading Activities

Upvotes: 6

Weird Workshop
Weird Workshop

Reputation: 43

Put your cleanup code in the onDestroy() method, this is last method that is called before an activity finally closes. for all the infomation on the Android activity life cycle have a look at Android lifecycle

Upvotes: 0

Related Questions