简体   繁体   中英

How to open main activity when application was killed?

A = Main Activity
B = Some Activity

  1. Normal case
    A > B > Home Button > B

  2. Application was killed case
    A > B > Home Button > idle for some time > application killed > open app again > A

How can I do like case 2 ?

PS I tried to add android:launchMode="singleTask" in Manifest. It always start at A but I would like to start at A in case 2 (application was killed) only.

Use this lines in your Application class. This code will automatically redirect your app to your desired page. if it get killed.

public class MYAppApplication extends Application {

    Context context;



    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();


        context=this;

        MySafetyMethod();
    }


    private void MySafetyMethod() {
        // TODO Auto-generated method stub
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread thread, Throwable ex) {                

                System.out.println("inside the process of handling exceptions");
                System.err.println("inside the process of handling exceptions");
                ex.printStackTrace();
                System.exit(2);

                startActivity(new Intent(context, YourActivity.class)); 

            }
        });
    }


}

hope this helps you. and also dont forget to mention the application name in your manifest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM