当应用程序崩溃,会发生什么? [英] What happens when App crashes?

查看:147
本文介绍了当应用程序崩溃,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,并如预期,在有些情况下我的code崩溃(错误,空指针等)。

I'm developing an Android app and as expected, there are situations where my code crashes (bugs, null pointers, etc.).

当应用程序崩溃,它将重新启动本身,即使我没有写任何code做到这一点(我想这是默认的行为?!),但问题是,应用程序是开放的不正确的状态

When the app crashes, it restarts itself, even though I haven't written any code to do it (I imagine it's the default behavior?!) but the problem is, the app is opening in an incorrect state.

下面就是通常发生在应用程序启动:

Here's what generally happens when the app starts up:

  • 首先,应用程序打开一个闪屏。该屏幕尝试使用坚持共享preferences标记并从服务器加载一些默认值,并坚持他们的一些服务,对用户进行认证。
  • 根据登录成功,要么进入到主屏幕或登录屏幕。

在应用程序正常启动,这一切工作正常,并执行初始化序列如预期,但是当它被重新作为一个崩溃的结果,这将已经填充在启动画面片段中的值(如登录用户或装载从服务器的默认值)均无效,这意味着它们是从不执行。

When the app starts normally, it all works fine and initialization sequence is executed as expected but when it is restarted as a result of a crash, the values that would've been populated in the splash screen fragment (such as logging in the user or loading the default values from the server) are all invalid, meaning they are never executed.

这使我相信,当应用程序重新启动本身是事件的顺序完全错误的,即不是重新开始使用正确的活动应用程序,在清单中指定的启动器,它似乎正确地重新启动的事情,从一开始错误的活动。

This leads me to believe that the sequence of events when the app restarts itself are completely wrong, i.e. instead of restarting the app using the correct activity, specified as Launcher in the manifest, it seems to restart things incorrectly and start from the wrong activity.

因此​​,这里的问题:什么决定了我的应用程序应该自己重新启动时崩溃发生什么决定应该如何开始呢?是否有任何事件时,应用程序从崩溃启动,这样我可以重写初始化序列我能赶上?有没有办法强制应用程序总是从启动活动的开始?

So here's the question: What determines that my app should restart itself when a crash happens and what determines how it should be started up? Are there any events I could catch when the app starts up from a crash so that I can override the initialization sequence? Is there a way to force the app to always start from the launcher activity?

在预先感谢,

推荐答案

在活动重新启动后,由于系统崩溃,如果运行前台活动的过程中消失了,系统会扔掉的活动,如果没有它有效的保存状态(通常这意味着它已暂停,并给出了系统的onSaveInstanceState的结果,从暂停之前)。一旦系统已决定是否要扔掉该活动,将恢复的任何活动,现在是在堆栈的顶部。如果这是你的活动之一,或者是因为你有另外身后的一个崩溃,还是一个坠毁在某种程度上它的入驻暂停状态,然后系统会重新启动您的进程表明,顶。你可以尝试设置 clearTaskOnLaunch =真正的在AndroidManifest主要活动(发射活动)的声明和安卓finishOnTaskLaunch =真正的对于除主要的一个其他的活动,看看这个prevents来自其他国家崩溃后重新启动应用程序。另一种方法是检查您的活动正在重新启动,然后检查的起点,本次活动拥有所有需要的数据或没有。如果它不具有所需的所有数据,然后重新定向到发射器的活动。希望这有助于。

When an activity is restarted, due to a crash, 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 the system 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 the system will start your process again to show that top activity. You can try setting clearTaskOnLaunch="true" in AndroidManifest main activity (launcher activity) declaration and android:finishOnTaskLaunch ="true" for other activities except main one and see if this prevents app from restarting at other state after a crash. Another way would be to check where your activity is restarting then check at start point that this activity has all required data or not. If it does not have all required data then redirect to launcher activity. Hope this helps.

这篇关于当应用程序崩溃,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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