Android Activity Stack 未按文档中的说明工作 - 未显示任务堆栈中的最后一个活动 [英] Android Activity Stack is not working as stated in the docs - last activity in task stack not shown

查看:17
本文介绍了Android Activity Stack 未按文档中的说明工作 - 未显示任务堆栈中的最后一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Android 文档:

According to Android docs:

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

"当用户按下 Home 按钮离开任务时,当前活动停止,其任务进入后台.系统保留任务中每个活动的状态.如果用户稍后通过选择恢复任务启动任务的启动器图标,任务进入前台并恢复堆栈顶部的活动."

"When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack."

如果我理解正确,这意味着:

If I understand this correctly, this means:

  1. 活动 A 作为主要活动.
  2. 通过startActivity"在 A 中启动的 Activity B - 常见的、普通的 Intent 实例.
  3. 我第一次打开应用程序,A 被加载.
  4. 我点击A中的按钮,B是openend.
  5. 我按下主页按钮.
  6. 我第二次再次打开应用,预计会显示 B

...对吗?我想这是期望的正确行为......

...right? I suppose this is the correct behavior to expect...

但是,我没有在我的应用中看到这个.

However, I am not seeing this in my app.

如果我点击主页按钮"然后通过按下启动器图标恢复我的应用程序,它将从主要活动开始 - 而不是顶部的活动或最新的活动.

If I hit "Home button" and then resume my app, by pressing the launcher icon, it will start with the main activity - not the one at the top or latest one.

我在三星 Galaxy Tab Android 2.2.1 上进行编码 - 我在 Android 清单中有最常见的选项 - 事情是我处理了 10 个不同的活动,具有不同的意图额外内容 - 以及一个 Dispatcher 类方法 - 或保存每个活动状态 - 听起来要求很高.

I am coding on a Samsung Galaxy Tab Android 2.2.1 - I have the most common options in the Android manifest - thing is that I handle like 10 different activities with different intent extras - and a Dispatcher class approach - or save each activity state - sounds quite demanding.

我正在使用带有 ADT 版本 12 的 Eclipse IDE;我发现了一些非常有趣的东西:

I am using Eclipse IDE with ADT version 12; and I found something very interesting:

当我从 Eclipse IDE 运行应用程序并连接我的设备时,我看不到这种行为.该应用程序的行为如文档中所述.事实上,我是在将我的 apk 部署到 Google Play 应用程序存储库后才看到这一点的;下载下来测试一下.

When I run the app from the Eclipse IDE, with my device connected, I don't see this behavior. The app behaves as stated in the docs. In fact, I saw this only after I deployed my apk at the Google Play app repository; and downloaded it to test.

我的问题是,有没有人找到发生这种情况的真正原因?文档有错吗?还是遗漏了什么?这是 Android 上的错误吗?

My question is, has anybody found the real reason why is this happening? Is the documentation wrong? or missing something? Is this a bug on Android?

我做过的另一项研究是:

Another research I have done is:

当我尝试从 google play 下载的 APK 应用程序时,如果我第二次进入我的应用程序,我会得到主要"活动,而不是最后一个 openend.我按回家.按主页后,我进入android的应用程序管理设置,找到我的应用程序并单击强制停止".执行此操作后,应用程序的行为如文档中所述.

When I try my app, downloaded from the google play, as APK, if I enter my app for the 2nd time, I get the "main" activity instead of the last one openend. I press home. After pressing home, I enter application management settings for android, locate my app and click on "force stop". After doing this, the app behaves as stated in the docs.

有人帮忙!:)

推荐答案

这是android平台的bug:

This is a bug in android's platform:

http://code.google.com/p/android/issues/detail?id=2373

解决方法是将其放在主 Activity 的 onCreate 方法中:

The workaround is, to place this in the onCreate method of your main Activity:

if (!isTaskRoot())
{
    final Intent intent = getIntent();
    final String intentAction = intent.getAction(); 
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN))
    {
        Log.w(LOG_TAG, "Main Activity is not the root.  Finishing Main Activity instead of launching.");
        finish();
        return;       
    }
}

摘自:

如何防止以不同意图启动的活动出现多个实例

...花了 3 天时间寻找这个.

...spent 3 days looking out for this.

这篇关于Android Activity Stack 未按文档中的说明工作 - 未显示任务堆栈中的最后一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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