Skip to content Skip to sidebar Skip to footer

Which Activity Is Called When App Removed From Recent List In Android?

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 a

Solution 1:

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.


Post a Comment for "Which Activity Is Called When App Removed From Recent List In Android?"