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.
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 the Exception
and otherwise we would not get the expected behavior
Try to handle any uncaught Exception and tell the system what to do. To implement this, try the below steps.
Application
ClassuncaughtException
in your Application
subclass.Activity
, call your Application
class.Activity
(as per your requirement).Step 1 and 2
package com.casestudy.intentsandfilter; import android.app.Application; import android.content.Intent; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Thread.setDefaultUncaughtExceptionHandler( new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException (Thread thread, Throwable e) { handleUncaughtException (thread, e); } }); } private void handleUncaughtException (Thread thread, Throwable e) { // The following shows what I'd like, though it won't work like this. Intent intent = new Intent (getApplicationContext(),DrawView.class); startActivity(intent); // Add some code logic if needed based on your requirement } }
Step 3
public class MainActivity extends Activity { protected MyApplication app; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get the application instance app = (MyApplication)getApplication(); ............. } }
Step 4
Modify the below method as per your requirement
private void handleUncaughtException (Thread thread, Throwable e) { // The following shows what I'd like, though it won't work like this. Intent intent = new Intent (getApplicationContext(), HomeActivity.class); startActivity(intent); // Add some code logic if needed based on your requirement }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With