Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

android - What happens when App crashes?

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

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:

  • Initially, the app opens with a "splash screen". This screen attempts to authenticate the user using persisted tokens in shared preferences and loading some default values from the server and persisting them in some services.
  • Depending on the login success, it either goes to the main screen or the login screen.

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?

Many thanks in advance,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...