从Android最近的列表中删除应用程序时调用哪个活动?

时间:2017-02-22 06:34:53

标签: android android-layout android-studio

android项目中有几个活动。但是,当从应用列表中删除应用时,肯定会调用哪个活动。

我检查过以下问题。但所有这些问题都是针对某项活动或服务的

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

1 个答案:

答案 0 :(得分:0)

我也陷入了非常类似的情况,我需要跟踪用户从最近的列表中移除应用的活动,所以移动到了这一点

第1步: 创建类ApplicationLifeCycleHandler并实现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
    }
}

}

现在 创建类MyApplication并将其扩展到Application

代码

public class MyApplication extends Application {

public static FileMetadata file;

@Override
public void onCreate() {
    super.onCreate();

    ApplicationLifeCycleHandler handler = new ApplicationLifeCycleHandler();
    registerActivityLifecycleCallbacks(handler);
    registerComponentCallbacks(handler);

}

}

第3步: 打开清单文件并添加android:name =" MyApplication"申请标签

第4步: 检查onActivityDestroyed的日志,您将知道被销毁的活动的名称