Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to cleanup application when it is killed

Tags:

java

android

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...

like image 391
Chen R Avatar asked Oct 24 '11 20:10

Chen R


People also ask

How do you handle running service when an app is killed by swiping in Android?

You can't handle swipe, because system just removes your process from memory without calling any callback. I have checked, that before user calls "recent apps" screen, onPause() will be always called. So you need to save all data in onPause method without checking isFinishing().

Is onDestroy called when app is killed?

When Android decides to kill our app, our activities will call onDestroy method. But before that, one more method will be also called and that is onSaveInstanceState(Bundle). If we check the first method, which is called when opening new activity, onCreate(Bundle), we can see the Bundle parameter.


2 Answers

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

like image 114
fadisdh Avatar answered Nov 14 '22 23:11

fadisdh


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.

like image 24
CommonsWare Avatar answered Nov 15 '22 00:11

CommonsWare