Best way to quit android app?

删除回忆录丶 提交于 2019-12-19 03:49:23

问题


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 customized flow that forces me to implement this. I've already been searching for a while, and found multiple answers:

  • It shouldn't be done => No option.
  • Call finish() in my activity => Only for 1 activity, not the whole app.
  • Start an Intent to the first activity with a special flag => I have multiple entrypoints, so not really an option.
  • Just kill my own process => Not sure about this one, will this call the onDestroy() method in all activities? And will my background services keep running with this? (Which shouldn't be stopped)

So I'm wondering, is there any other option to quit my app, or am I really limited to these options? If there really isn't an other option, then I was thinking to create a list of all the instances of my activities in my Application class, and then just loop them to call finish() on them. But I don't know if this will be the correct way to handle this.

So that's why I ask my question here, which is the best way to close my app?

Edit: I more or less fixed the problem. I have a list of WeakReference of my Activities. In every onCreate I add the activity to the list. Then, when I want to quit my app, I just loop the list and call finish(). Problem being: If the list gets too big, it won't finish all my activities, since android already destroyed them. So every time I know for sure I don't need them anymore, I finish them. In my case, the list can't grow bigger than 3/4 activities, so no need anymore to worry about activities not getting finished. Also with this method, I don't have to destroy my own process.


回答1:


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

System.exit(0);



回答2:


use below code.

android.os.Process.killProcess(android.os.Process.myPid());
    System.runFinalizersOnExit(true);



回答3:


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();
    }



回答4:


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());



来源:https://stackoverflow.com/questions/9532405/best-way-to-quit-android-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!