控制机器人的活动启动一个进程停止后, [英] Controlling Android Activity restart after a process stops

查看:101
本文介绍了控制机器人的活动启动一个进程停止后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是运行Android姜饼2.3.7

My app is running on a special device that is running a custom build of Android Gingerbread 2.3.7

在某些条件下,系统将终止我应用程序。我认为设备制造商认为,所有的第三方应用程序,应立即关闭这些紧急情况下这样的设备可以做它的主要任务。

There are conditions where the system will terminate my app. I assume that the device manufacturer considers these emergency situations where all third party apps should be immediately shut down so the device can do its primary tasks.

我可以复制我选择我的任务,点击停止进程按钮,使用模拟器在设备上,在DDMS看到的行为。这是我看到的行为。

I can duplicate the behavior I see on the device using an emulator and in DDMS selecting my task and clicking the "Stop Process" button. Here is the behavior I see.

我的应用程序通常是通过四个活动步骤,活动一推出活动B,B发射活动C,和C启动活动D.因此,当活动D被跑在最前面,我的筹码是:

My app typically steps through four activities, Activity A launches activity B, B launches Activity C, and C launches activity D. So, when activity D is running on top, my stack is:

一个 - B - C - ð

A - B - C - D

如果在这一点上,该过程被终止,活动D不接收的onPause()或的onStop()调用。它有没有机会保存其状态。

If at this point, the process is terminated, Activity D does not receive onPause() or an onStop() call. It has no opportunity to save its state.

在这个过程已经死了,Android的ActivityManager启动一个新的任务,我的应用程序,并启动活动C.我觉得这是标准的Andr​​oid行为重新启动崩溃的应用程序。

After the process is dead, Android's ActivityManager starts a new task for my app and launches Activity C. I think this is standard Android behavior to restart crashed apps.

我的问题是,我可以控制这种重新启动行为?如果Android正在准备重新启动我的应用程序,我需要恢复活动栈,活动C没有真正意义的单独运行的立场(点击后退按钮将退出该应用程序并没有意义这项活动)。

My question is can I control this restart behavior? If Android is going to restart my app, I need the activity stack restored, Activity C doesn't really make sense to be run stand alone (clicking the back button would exit the app and that doesn't make sense for this activity).

我可以prevent这个重启? 我可以重新开始启动顺序我所有的活动? 我可以重新启动刚开始活动的一个?

Can I prevent this restart? Can I have the restart start all my activities in sequence? Can I have the restart just start activity A?

我没有找到这个有趣的讨论我相信解释了为什么活动C被重新启动,而不是活动D.

I did find this interesting discussion which I believe explains why Activity C is restarted and not Activity D.

至于活动重新启动时 - 如果进程运行   前台活动消失,系统会扔掉   活动,如果它不具有一个有效的保存状态(通常   这意味着它已暂停,并给出了系统的结果   的onSaveInstanceState从暂停之前)。一旦它已决定   是否要扔掉的活动,将恢复一切   活动是现在在堆栈的顶部。如果这是一个你   活动 - 要么是因为你有另外背后的一个   崩溃,或者一个坠毁在某种程度上它的入驻暂停   状态 - 然后再重新启动过程中显示顶部   活动。

As far as when an activity is restarted -- if the process running the foreground activity goes away, the system will throw away that activity if it does not have a valid saved state for it (typically meaning it is paused and has given the system the result of onSaveInstanceState from before the pause). Once it has decided whether or not to throw away that activity, it will resume whatever activity is now at the top of the stack. If this is one of your activities -- either because you have another behind the one that crashed, or the one that crashed was somehow it the settled pause state -- then it will start your process again to show that top activity.

和一些类似的问题,像<一个href="http://stackoverflow.com/questions/5423571/$p$pvent-activity-stack-from-being-restored">$p$pvent活动堆栈进行恢复?以及这个有趣的线程

And some similar questions like Prevent Activity Stack from being Restored? and this interesting thread

推荐答案

很多实验后,我选择了办法,Android开发者>的 OS活动后重启死机杀死它。一个Q / A交汇处有:

After a lot of experimenting, I opted for the approach hinted at in "Android Developers › Activity restart crash after OS kills it". One Q/A interchange there was:

问:

已通过code加大,并且已经看见你在说   关于。它调用的onSaveInstanceState并保存在包中的数据。   这是否会捆绑信息提供活动时重新启动   操作系统杀死后工序?

"have stepped through the code and have seen what you are talking about. It calls onSaveInstanceState and saves the data in the Bundle. Will this Bundle information be available when the activity restarts after the OS kills the process?"

答:

这是应该。你会得到这个包中的onCreate()和   onRestoreInstanceState()。的onCreate()可以为传递空   束,如果没有包来恢复。 onRestoreInstanceState()是   只有当有一个包还原之称。

"It's supposed to. You will get that Bundle in onCreate() and in onRestoreInstanceState(). onCreate() can be passed null for the Bundle, if there is no Bundle to restore. onRestoreInstanceState() is only called when there is a Bundle to restore."

我搬到了恢复后的应用程序是杀入一个序列化的单所需的所有会话数据。

I moved all the session data needed to recover after the app is killed into a serializable singleton.

在的onSaveInstanceState()我把序列化会话数据到savedInstanceState包

In onSaveInstanceState() I put the serialized session data in to the savedInstanceState bundle

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // save and restore session data to instance state to recover from task termination 
    savedInstanceState.putSerializable(SessionVariables.class.getName(), mSessionVariables);        
}

在的onCreate()和onRestoreInstanceState()我测试一下,看看如果我的会话一个实例是有效的。如果没有有效的数据,我从savedInstanceState包恢复会话变量,并把这个恢复的对象作为我的会话变量单。

In onCreate() and onRestoreInstanceState() I test to see if my session singleton instance is valid. If it doesn't have valid data, I restore the session variables from the savedInstanceState bundle and put this restored object as my session variable singleton.

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if (mSessionVariables.propertyThatShouldbeGood == null || mSessionVariables.propertyThatShouldbeGood .length() == 0)
    {
        // save and restore session data to instance state to recover from task termination
        Serializable serializedSessionVariables = savedInstanceState.getSerializable(SessionVariables.class.getName());

        if (serializedSessionVariables != null) {
            mSessionVariables = (SessionVariables) serializedSessionVariables;
            SessionVariables.putInstance(mSessionVariables);
        }
    }
}

现在,当我的任务就是杀死,Android的恢复从栈中previous活动。该的onCreate()从所保存的实例状态包恢复所有必要的会话数据。

Now when my task is killed, Android restores the previous activity from the stack. The onCreate() restores all the necessary session data from the saved instance state bundle.

后退按钮也是在这一点上工作得很好,所以我错了Android的不是preserving活动堆栈。我认为它只是恢复回活动需要(当您导航回)。如果你不导航回,它不创建他们。至少,这是它看上去就像HierarchyViewer。

The Back button also works fine at this point, so I was wrong Android not preserving the Activity stack. I think it just restores the back activities as needed (when you navigate back). If you don't navigate back, it doesn't create them. At least that is what it looked like in HierarchyViewer.

在我的筹码是ABCD,任务被杀死,活动的C恢复和D将丢失。不过,现在的C是在一个健康的状态,用户可以轻松地浏览回D。现在,我还可以自动从C开始的D的活动,我只是还没有来得及呢。

When my stack is A-B-C-D and the task is killed, Activity "C" is restored and "D" is lost. But, now "C" is in a healthy state and the user can easily navigate back to "D". Now, I could also start the "D" activity automatically from "C", I just haven't had time yet.

这篇关于控制机器人的活动启动一个进程停止后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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