Best way to quit android app?

后端 未结 4 1972
误落风尘
误落风尘 2021-01-06 05:29

I\'m looking for a way to quit my android app by code. Yes, I know, I shouldn\'t be doing this cause android handles it when you press the back button, but I have a customiz

相关标签:
4条回答
  • 2021-01-06 05:55

    You can keep a list of all started activities in List (use WeakReference to avoid memory leak). To exit the app, first invoke finish method on each items and then invoke android.os.Process.killProcess(android.os.Process.myPid());

    0 讨论(0)
  • 2021-01-06 06:07

    You should not do this, but anyways if you insist:

    System.exit(0);
    
    0 讨论(0)
  • 2021-01-06 06:18

    use below code.

    android.os.Process.killProcess(android.os.Process.myPid());
        System.runFinalizersOnExit(true);
    
    0 讨论(0)
  • 2021-01-06 06:22

    Not recommened but still you can use this. Better go with this solution in case you need to quit the app.

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

    step1) maintain a static variable in mainactivity say.

    public static isQuit = false;
    

    step2) on click event of an button make this variable to true.

     mainactivity.isQuit = true;
    

    step3) And in every activity of your application have onrestart method as below..

     @Override
          protected void onRestart() {
             // TODO Auto-generated method stub
             super.onRestart();
            if(mainactivity.isQuit)
                finish();
        }
    
    0 讨论(0)
提交回复
热议问题