I have an app and if the app crashes in a particular activity, it restarts at one of the intermediate parent activities.
This is a problem for me since I have some inputted user info that is lost upon crash.
Is there any way to force the app to start from the launcher screen after restarting from a crash?
Thanks.
Proposed Solution 1 -
Add this tag
android:clearTaskOnLaunch="true"
in the manifest.xml file to your main activity which should always launch.Probable Reason why it did not work
When the application crashes, it throws an
Exception
and we need to handle theException
and otherwise we would not get the expected behaviorProposed Solution 2
Try to handle any uncaught Exception and tell the system what to do. To implement this, try the below steps.
Application
ClassuncaughtException
in yourApplication
subclass.Activity
, call yourApplication
class.Activity
(as per your requirement).Code Sample
Step 1 and 2
Step 3
Step 4
Modify the below method as per your requirement
first create and set the
App
class in yourAndroidManifest.xml
andthen put this code in the App class
Debug Log Screen Shoot
Maybe there's no way to do that but you can flag it so you know if the activity was started through user action or if it was just started after a crash.
i.e when you start the parent activity, pass something into the startActivity intent. If that isn't there then it was started after the crash.
I would recommend using library such as
https://github.com/Ereza/CustomActivityOnCrash
As the library takes care of other stuff along with different versions of android.
I managed to start my main activity with
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
like this: