活动堆排序问题,当从推出Android应用程序安装程序,并从主屏幕中的应用 [英] Activity stack ordering problem when launching application from Android app installer and from Home screen

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

问题描述

仅用于测试目的,我让我的应用程序APK通过URL来下载和安装。一旦下载在手机上,它可以与Android应用程序安装它使用​​户能够将其安装到自己的设备,然后运行它的一个选项来启动。

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.

试想,如果我们下载并运行在上述方法的应用程序。在我的应用程序的主/发射活动是一个登录页面(活动A )。一旦用户被认证,就被带到该应用程序,例如主区 b活动。所以,现在这个任务的当前活动堆栈 A>乙

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.

我那么preSS手机上的home键和我带到Android主屏幕。我通过菜单中的图标重新启动我的应用程序,而我带到活动A 而不是,b活动。无论是活动堆栈现在 A> B> A ,或者有现已与活动栈 A&GT两个独立的任务;乙 A 分别。我想是要被带回 b活动当我重新启动应用程序。 pressing回来,而在这种状态下会带我回 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] }

当与我们看到它正在使用的标志 0x10000000处,但与启动器启动,当我们看到它是使用 0x10200000 。它也使用意图类别

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.

在<一个href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK">docs我们看到的标志是:

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)

标志 FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (当应用程序被从发射装置正在被使用),似乎通常prevent从已经存在的情况正在创建新任务,并恢复到上次使用的活动。这是所期望的行为。为什么不工作在这种情况?有什么我可以做,以确保我的应用程序将总是返回我的最后一项活动,无论它是否通过应用程序安装/启动器启动?

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?

如果我用 singleTask 它总是会带我回主要活动(活动A ),每当我跑该应用(这也是不希望的)。

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).

下面是一个问题,我发现那里有人正在经历(没有公认的答案)一个类似的问题:<一href="http://stackoverflow.com/questions/5318885/app-loses-its-ability-to-remember-its-stack-when-launched-from-another-applicatio">App失去了它的能力,记住它从另一个应用程序启动时堆栈。

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.

编辑:检查在的onCreate()我们的发射活动,(然后完成标志 FLAG_ACTIVITY_BROUGHT_TO_FRONT 如果设置)似乎解决的主要症状,但很明显,潜在的问题仍然存在。是否有一个更完整的修补程序?

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:当你下载/运行从Android Market的应用程序,所以上面的一些细节可能不相关时,会出现相同的结果。

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

推荐答案

补充说明antonyt提供了答案:

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天全站免登陆