Sree
Sree

Reputation: 2787

Is there a way to restart the application instead of having it restart the last activity after a crash?

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

Answers (4)

CarlesCF
CarlesCF

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:

  • Finishing the activity to go back to your activity A
  • Launching another activity
  • Asking the user for feedback
  • Any other action you think fits best

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

AnxGotta
AnxGotta

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

HappyCactus
HappyCactus

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

jlindenbaum
jlindenbaum

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

Related Questions