当用户通过单击主屏幕启动器打开应用程序时,Android 如何确定要恢复的 Activity/任务?我首先从软件包安装程序启动应用程序,然后从主屏幕启动器启动应用程序时遇到了问题。
步骤:
- 使用软件包安装程序从 APK 安装应用。
- 安装后,从软件包安装程序中打开该应用。
- 按主页按钮
- 在主屏幕上,点击应用图标
问题:
MainActivity 在堆栈上出现两次。使用该应用程序的任何人都需要按两次后退按钮才能退出。我已经能够使用 Android Studio 中的新建项目向导成功重现此问题,并进行以下更改:Min SDK=14、Target SDK=17、Compile SDK=17)。
使用 dumpsys(adb shell dumpsys Activity ),我在包安装程序运行后记录了堆栈,并在从主屏幕启动器启动应用程序时再次记录了堆栈。
从软件包安装程序中单击“打开”后:
Task id #99
TaskRecord{424f0e10 #99 A=com.example U=0 sz=1}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.example cmp=com.example/.MainActivity }
Hist #0: ActivityRecord{4231c6e8 u0 com.example/.MainActivity t99}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.example cmp=com.example/.MainActivity }
ProcessRecord{42458650 16816:com.example/u0a294}
按主屏幕按钮后,然后从主屏幕启动器启动应用程序:
Task id #99
TaskRecord{424f0e10 #99 A=com.example U=0 sz=2}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.example cmp=com.example/.MainActivity }
Hist #1: ActivityRecord{431e05b8 u0 com.example/.MainActivity t99}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10600000 cmp=com.example/.MainActivity }
ProcessRecord{42458650 16816:com.example/u0a294}
Hist #0: ActivityRecord{4231c6e8 u0 com.example/.MainActivity t99}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.example cmp=com.example/.MainActivity }
ProcessRecord{42458650 16816:com.example/u0a294}
很明显,当从主屏幕启动时,主屏幕启动器确实会恢复现有的 MainActivity。 这是我最初的假设。我认为主屏幕启动器会检测现有 Activity 并恢复该任务。
我已在 Android 4.0.4 (Samsung Galaxy GT-N7000) 和 Android 4.4.2 (HTC One) 上对此进行了测试,并看到了相同的结果。
如果您能提供有关此问题的解决方案或解决方法的任何帮助,以及描述此 Android 行为的任何文章/文档,我们将不胜感激。
最佳答案
你并不孤单。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
}
参见http://gosyujin.github.io/2013/08/04/android-install-intent是用日语写的。 这对我有用。
关于android - Android 主屏幕启动器如何恢复任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23880318/