onDestory 流程

2020-08-18  本文已影响0人  xuefeng_apple

问题:在原生android 界面中有三个按键:
1-左边的是返回键
2-中间是home键(主页)
3-右边为返回后台运行键,可以切换多任务

当前Activity在运行的时候,按home 按键, 流程走到的onPause-->onStop-->onDestory
这个就是问题,怎么走到的onDestory? 由于app 在onDestory 做了一些逻辑, 因此影响比较大

走到onDestory 原因是:mAlwaysFinishActivities=true , 这个设定可以在setting中设定,设定位置为下图:


图片.png

在程序中调用参考:

 读取 不保留活动的 状态值  Settings.System.getInt(getContentResolver (),Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);

跳转到开发者选项
Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS );
startActivity(intent);

文件路径

frameworks\base\core\java\android\app\Activity.java
frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java
frameworks\base\services\core\java\com\android\server\am\ActivityStack.java
frameworks\base\core\java\android\app\ActivityThread.java
frameworks\base\core\java\android\app\IApplicationThread.aidl
frameworks\base\core\java\android\app\Instrumentation.java

流程分析:onDestory 有两种情况
1:finish-->onDestroy, 正常情况都走这个
2:scheduleDestroyAllActivities-->onDestroy,使用上图设定就会走这个,isfinish=false

第一种情况:
-->finish//【Activity】
   --->ActivityManagerNative.getDefault().finishActivity//【AMS】
       ---> tr.stack.requestFinishActivityLocked//【ActivityStack】
            --> finishActivityLocked
            -->finishCurrentActivityLocked
            --> destroyActivityLocked
                    -->  r.app.thread.scheduleDestroyActivity//【ApplicationThread】                        
                          -->    handleDestroyActivity//【ActivityThread】
                           -->    performDestroyActivity
                                   ---->mInstrumentation.callActivityOnDestroy//【Instrumentation】
-->performDestroy//【Activity】
-->onDestroy

第二种情况:
updateOomAdjLocked//【ActivityManagerService.java】
   ----->mAlwaysFinishActivities
   ----->mStackSupervisor.scheduleDestroyAllActivities(null, "always-finish");---》出现问题时候状态
         ----->scheduleDestroyAllActivities//【ActivityStackSupervisor.java】
                  ----->scheduleDestroyActivities //【ActivityStack.java】
                         ----->DESTROY_ACTIVITIES_MSG:sendMessage
                        ---->handleMessage:case DESTROY_ACTIVITIES_MSG
                        ---->destroyActivitiesLocked-->destroyActivityLocked
                    ---->r.app.thread.scheduleDestroyActivity//【ApplicationThread】  
                                      -->    handleDestroyActivity//【ActivityThread】
                                      -->    performDestroyActivity
                                           -->mInstrumentation.callActivityOnDestroy//【Instrumentation】
-->performDestroy//【Activity】
-->onDestroy

如何调试:
frameworks/base/services/core/java/com/android/server/am /ActivityManagerDebugConfig.java
DEBUG_SWITCH = DEBUG_ALL || true
DEBUG_CLEANUP = DEBUG_ALL || true;

source 
lunch
make services

adb push out/target/product/sdm845/system/framework/services.jar system/framework/
adb push out/target/product/sdm845/system/framework/oat/arm64/services.odex system/framework/oat/arm64/
adb push out/target/product/sdm845/system/framework/oat/arm64/services.vdex system/framework/oat/arm64/
adb push out/target/product/sdm845/system/framework/oat/arm64/services.art  system/framework/oat/arm64/

出现问题时候的log:
ActivityManager: Removing activity from always-finish: token=ActivityRecord{71028b8 u0 com.example.helloworld/.MainActivity t1147}, app=com.example.helloworld

其他:
dumpsys package com.example.helloworld

上一篇下一篇

猜你喜欢

热点阅读