Luis
Luis

Reputation: 668

How to execute a piece of code when app closes?

I would like to know how i can execute a piece of code when the app closes. My objective is to send a cancel call to the notification so the notification is destroid when the app closes.

Upvotes: 2

Views: 141

Answers (2)

Luis
Luis

Reputation: 668

I found what i was looking for here , even though the process is not well documented, its a solution .

It basicly consists of creating a service that will execute an action when the the app is killed in the taskmanager.

Used this as an helper to know how to create a service and then used

@Override
public void onTaskRemoved(Intent rootIntent) {
    MainActivity.notification.onCancel();
    stopSelf();
    super.onTaskRemoved(rootIntent);
}

to kill the notification and stop the service when the app was closed in the task manager

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98871

Use the Activity onDestroy() method

@Override
public void onDestroy() {
    Log.d("SampleApp", "destroy");
    super.onDestroy();
}

Upvotes: 2

Related Questions