[英]Which activity is called when app removed from recent list in android?
There are several activities in the android project. android 项目中有几个活动。 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? 在android中通过滑动杀死应用程序时如何处理代码?
How to detect app removed from the recent list 如何检测从最近列表中删除的应用程序
What method is being called when I close an app 关闭应用程序时调用了什么方法
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:第1步:
Create Class ApplicationLifeCycleHandler
and implement Application.ActivityLifecycleCallbacks
, ComponentCallbacks2
:创建类
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
}
}
}
Now: Create Class MyApplication
and extend it to Application:现在:创建类
MyApplication
并将其扩展到应用程序:
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.第 3 步:打开清单文件并将
android:name="MyApplication"
到应用程序标签。
Step 4 : Check Logs of onActivityDestroyed
and you will know the name of activity which is getting destroyed.第 4 步:检查
onActivityDestroyed
日志,您将知道被销毁的活动的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.