0

There are several activities in the android project. But which activity will surely get called when the app is removed from the app list.

I have checked following questions. But all these questions are specific to an activity or service

How to handle code when app is killed by swiping in android?

How to detect app removed from the recent list

What method is being called when I close an app

Community
  • 1
  • 1
Samttha Biby
  • 73
  • 11
  • no such activity is called. But offcourse onDestroy method of all the activities is called which are in stack or running. – Ankush Bist Feb 22 '17 at 06:37
  • @AnkushBist Actually this is not true. Swiping app from the recent tasks list usually just causes Android to kill the OS process hosting the application and with it all activities, services, etc. In this case `onDestroy()` is not called at all. – David Wasser Mar 11 '19 at 15:46

1 Answers1

2

I also got stuck in very much similar scenario, where I need to trace from which activity the user removed app from recent list, So moving to the point

Step 1:

Create Class ApplicationLifeCycleHandler and implement Application.ActivityLifecycleCallbacks, ComponentCallbacks2:

public class ApplicationLifeCycleHandler implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
  private static final String TAG = "AppLifeCycleShareTime";
  private static boolean isInBackground = false;

  @Override
  public void onActivityCreated(Activity activity, Bundle bundle) {
    Log.d( TAG  , "onActivityCreated");
  }

  @Override
  public void onActivityStarted(Activity activity) {
    Log.d( TAG  , "onActivityStarted");
  }

  @Override
  public void onActivityResumed(Activity activity) {
    Log.d(  TAG , "onActivityResumed : " + ShareTime.currentActivity.getClass().getName());
    if(isInBackground){
      Log.d(TAG, "app went to foreground");
      isInBackground = false;
    }
  }

  @Override
  public void onActivityPaused(Activity activity) {
    Log.d(  TAG  , "onActivityPaused : " + activity.getClass().getName());
  }

  @Override
  public void onActivityStopped(Activity activity) {
    Log.d( TAG , "onActivityStopped : " + activity.getClass().getName());
  }

  @Override
  public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    Log.d(  TAG  , "onActivitySaveInstanceState");
  }

  @Override
  public void onActivityDestroyed(Activity activity) {
    Log.d( TAG , "onActivityDestroyed Parent : " + activity.getClass().getName());
  }

  @Override
  public void onConfigurationChanged(Configuration configuration) {
    Log.d( TAG , "onConfigurationChanged");
  }

  @Override
  public void onLowMemory() {
    Log.d( TAG , "onLowMemory");
  }

  @Override
  public void onTrimMemory(int i) {
    Log.d( TAG  , "onTrimMemory");
    if(i == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN){
      Log.d(TAG, "app went to background");
      isInBackground = true
    }
  }
}

Now: Create Class MyApplication and extend it to Application:

public class MyApplication extends Application {
  public static FileMetadata file;

  @Override
  public void onCreate() {
    super.onCreate();
    ApplicationLifeCycleHandler handler = new ApplicationLifeCycleHandler();
    registerActivityLifecycleCallbacks(handler);
    registerComponentCallbacks(handler);
  }
}

Step 3 : Open Manifest File and Add android:name="MyApplication" to application Tag.

Step 4 : Check Logs of onActivityDestroyed and you will know the name of activity which is getting destroyed.

Igor F.
  • 2,649
  • 2
  • 31
  • 39