Skip to content
Advertisement

Call method when Application Closes

I know this is asked many times, but I want to clear things up

In which activity should I place onDestroy()? Say I am in the MainActivity A and I move to activity B. But then I close the application while being on activity B. Does the onDestroy() method in MainActivity A get called? Or should I put an onDestroy() method for every activity in the application? Or is there any other way around this?

I have tried reading documentation and reading other answers but they don’t help me clearly.

Advertisement

Answer

There are multiple ways to do so , Here I have mentioned the most reliable way I have found during development carrier . You need to create a Service which can keep an eye whether application got closed or not, so here I have mentioned the code !

1.Create Service , as you can see below there is a new class named as MyService, this needs to extended to Service.

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.d(getClass().getName(), "App just got removed from Recents!");
    }


}

2.Add this entry to Manifest.xml as below …

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TestApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" />
    </application>

</manifest>

3.This is Just a Service that will run in background , but we need to start it once the application starts , so in your launcher activity start it like below.

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Intent stickyService = new Intent(this, MyService.class);
        startService(stickyService);

    }
}

4.Service will start and whenever User will Swipe out the application from recent , the function onTaskRemoved will be called automatically , in this function you can put your required work or code to be executed at the application end !

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement