从 Android 应用安装程序和主屏幕启动应用程序时的活动堆栈排序问题 [英] Activity stack ordering problem when launching application from Android app installer and from Home screen

查看:24
本文介绍了从 Android 应用安装程序和主屏幕启动应用程序时的活动堆栈排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅出于测试目的,我允许通过 URL 下载和安装我的应用 APK.在手机上下载后,可以使用 Android 应用安装程序启动它,让用户可以选择将其安装到他们的设备上,然后运行.

考虑我们是否以上述方式下载并运行了该应用程序.我的应用程序中的主/启动器 Activity 是一个登录页面(Activity A).一旦用户通过身份验证,他们就会被带到应用程序的主要区域,例如活动B.所以现在这个任务的当前活动栈是 A >B.

然后我按下手机上的主页按钮,进入 Android 主屏幕.我通过菜单中的图标重新启动我的应用程序,然后我被带到 Activity A,而不是 Activity B.活动堆栈现在是 A >乙>A,或者现在有两个单独的任务,活动堆栈 A >BA 分别.我想要的是在我重新启动应用程序时返回到 Activity B.在此状态下按回将带我回到 Activity B.

这种不良行为仅在我首先通过安装程序打开应用程序时发生,而不是在我通过主屏幕/菜单打开应用程序时发生.

我研究了每种机制如何启动活动.当我们使用应用安装程序时,我们会看到以下日志:

INFO/ActivityManager(XXXX):开始活动:Intent { dat=file:///mnt/sdcard/download/[my app].apk cmp=com.android.packageinstaller/.InstallAppProgress(有额外的) }INFO/ActivityManager(XXXX): 开始活动: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=[我的包]/[Activity A] }

通过启动器/主屏幕:

INFO/ActivityManager(XXXX):开始活动:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=[我的包]/[Activity一个] }

使用安装程序启动时,我们看到它使用标志 0x10000000,但使用启动器启动时,我们看到它使用 0x10200000.它还使用了一个意图类别.

docs 中,我们看到标志是:

public static final int FLAG_ACTIVITY_NEW_TASK常数值:268435456 (0x10000000)公共静态最终 int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED常数值:2097152 (0x00200000)

标志 FLAG_ACTIVITY_RESET_TASK_IF_NEEDED(在从启动器启动应用程序时使用)似乎通常会阻止创建新任务(如果新任务已经存在),并将恢复上次使用的活动.这是所需的行为.为什么在这种情况下它不起作用?我可以做些什么来确保我的应用程序始终将我返回到最后一个 Activity,而不管它是否是通过应用安装程序/启动程序启动的?

如果我使用 singleTask,每当我运行应用程序(这也是不可取的)时,它总会带我回到主要活动(Activity A).>

这是我发现有人遇到类似问题的问题(没有公认的答案):当从另一个应用程序启动时,应用程序失去了记住其堆栈的能力

在我们的启动器活动的 onCreate() 中检查标志 FLAG_ACTIVITY_BROUGHT_TO_FRONT(如果设置了则完成)似乎解决了主要症状,但很明显根本问题仍然存在.有更完整的修复吗?

当您从 Android Market 下载/运行应用程序时会出现相同的结果,因此上述某些细节可能不相关.

解决方案

添加了antonyt提供的答案:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);如果 ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {//活动被带到前面而不是被创建,//因此完成此操作将使我们进入上次查看的活动结束();返回;}//常规活动创建代码...}

For testing purposes only, I am allowing my app APK to be downloaded and installed via a URL. Once downloaded on the phone, it can be launched with the Android app installer which gives the user an option to install it to their device and then run it.

Consider if we downloaded and ran the app in the way described above. The main/launcher activity in my app is a login page (Activity A). Once the user is authenticated, they are taken to the main area of the application, e.g. Activity B. So now the current activity stack of this task is A > B.

I then press the home button on the phone and am taken to the Android home screen. I re-launch my app via the icon in the menu, and I am taken to Activity A, instead of Activity B. Either the activity stack is now A > B > A, or there are now two separate tasks with activity stacks A > B, and A respectively. What I want is to be taken back to Activity B when I relaunch the app. Pressing back whilst in this state will take me back to Activity B.

This undesired behaviour only happens if I first open the app via the installer, and not if I open the app via the home screen/menu.

I looked into how the activities are being started by each mechanism. When we use the app installer, we see the following logs:

INFO/ActivityManager(XXXX): Starting activity: Intent { dat=file:///mnt/sdcard/download/[my app].apk cmp=com.android.packageinstaller/.InstallAppProgress (has extras) }
INFO/ActivityManager(XXXX): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=[my package]/[Activity A] }

via launcher / home screen:

INFO/ActivityManager(XXXX): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=[my package]/[Activity A] }

When started with the installer we see it is using the flag 0x10000000, but when started with the launcher we see it is using 0x10200000. It is also using an intent category.

From the docs we see the flags are:

public static final int FLAG_ACTIVITY_NEW_TASK
Constant Value: 268435456 (0x10000000)

public static final int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
Constant Value: 2097152 (0x00200000)

The flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (which is being used when the app is launched from the launcher) seems to usually prevent a new task from being created if one already exists, and will restore the last used activity. This is the desired behaviour. Why is it not working in this situation? Is there anything I can do to make sure my application will always return me to the last Activity regardless of whether it was started through the app installer/launcher?

If I use singleTask it will always take me back to the main activity (Activity A) whenever I run the app (which is also not desirable).

Here is a question I found where someone is experiencing a similar problem (which has no accepted answer): App loses its ability to remember its stack when launched from another application

EDIT: Checking for the flag FLAG_ACTIVITY_BROUGHT_TO_FRONT in onCreate() of our launcher activity (and then finishing if it is set) seems to fix the main symptom, but clearly the underlying issue is still there. Is there a more complete fix?

EDIT2: The same result occurs when you download/run the app from the Android Market, so some of the above details may not be relevant.

解决方案

Added the answer that antonyt provided:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
        // Activity was brought to front and not created,
        // Thus finishing this will get us to the last viewed activity
        finish();
        return;
    }

    // Regular activity creation code...
}

这篇关于从 Android 应用安装程序和主屏幕启动应用程序时的活动堆栈排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆