Close application and launch home screen on Android

后端 未结 21 2043
我在风中等你
我在风中等你 2020-11-22 07:33

I have two different activities. The first launches the second one. In the second activity, I call System.exit(0) in order to force the application to close, bu

21条回答
  •  情深已故
    2020-11-22 08:01

    It is not recommended, but you can still use this. Better go with this solution in case you need to quit the app.

    According to me, the best solution is to finish every activity in your app like below.

    Step 1. Maintain a static variable in mainactivity. Say,

    public static boolean isQuit = false;
    

    Step 2. On click event of an button, set this variable to true.

    mainactivity.isQuit = true;
    finish();
    

    Step 3. And in every activity of your application, have the onrestart method as below.

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        if(mainactivity.isQuit)
            finish();
    }
    

提交回复
热议问题