Reputation: 2787
Since I store certain information that is necessary in my Application class, when a user encounters a crash, sometimes they encounter two crashes since the last activity needs information stored in the application class.
Activity A >> Activity B (needs info from Application class) >> Activity C
Crash occurs on C >> Application tries to open Activity B >> Crash occurs on B because it needs info from the Application class (this has been reset)
I want to avoid this, so I was hoping that there is someway I can restart the whole application after a crash instead of the activity.
Upvotes: 0
Views: 104
Reputation: 205
The nicest solution is to handle the case in which you do not have the necessary information at any activity (in your case Activity B).
You should check at the onCreate method whether you have the information or not, and act in consecuence:
There may be more than one reason explaining why you don't have that information (as other answers point out), so in my opinion the best option is to handle the case.
Upvotes: 2
Reputation: 1006
This is one possible solution of many but I'll put it here as an option:
You could use startActivityForResult()
and when you handle the error in Activity C, finish()
it with a result code that you recognize as a crash and handle things appropriately in the onActivityResult()
of Activity B.
Getting a Result from an Activity
Upvotes: 2
Reputation: 2014
You shouldn't keep any state in the Application object. Keep it in the Bundle you receive in the onCreate(). Eventually use onSaveInstanceState / onRestoreInstanceState to save/restore state.
Upvotes: 1
Reputation: 1881
Untested theory, just spitballing.
Could you attach yourself as a default exception handler, and start calling finish()
on the open activities? If you can finish all your activities, wouldn't that effectively shut down the app, and reopening it would be as if it were getting launched fresh?
Upvotes: 1