Close application and launch home screen on Android

后端 未结 21 1754
我在风中等你
我在风中等你 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:04

    This is what I did to close the application: In my application I have a base activity class, I added a static flag called "applicationShutDown". When I need to close the application I set it to true.

    In the base activity onCreate and onResume after calling the super calls I test this flag. If the "applicationShutDown" is true I call finish on the current Activity.

    This worked for me:

    protected void onResume() {
        super.onResume();
        if(BaseActivity.shutDownApp)
        {
            finish();
            return;
    
        }}
    
    0 讨论(0)
  • 2020-11-22 08:05

    You can not do System.exit(), it's not safe.

    You can do this one: Process.killProcess(Process.myPid());

    0 讨论(0)
  • 2020-11-22 08:07

    android.os.Process.killProcess(android.os.Process.myPid()); works fine, but it is recommended to let the Android platform worry about the memory management:-)

    0 讨论(0)
  • 2020-11-22 08:07

    Start the second activity with startActivityForResult and in the second activity return a value, that once in the onActivityResult method of the first activity closes the main application. I think this is the correct way Android does it.

    0 讨论(0)
  • 2020-11-22 08:10

    Use the finish method. It is the simpler and easier method.

    this.finish();
    
    0 讨论(0)
  • 2020-11-22 08:11

    I use this:

    1) The parent activity call the secondary activity with the method "startActivityForResult"

    2) In the secondary activity when is closing:

    int exitCode = 1; // Select the number you want
    setResult(exitCode);
    finish();
    

    3) And in the parent activity override the method "onActivityResult":

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        int exitCode = 1;
        if(resultCode == exitCode) {
            super.setResult(exitCode); // use this if you have more than 2 activities
            finish();
        }
    }
    

    This works fine for me.

    0 讨论(0)
提交回复
热议问题