Close application and launch home screen on Android

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

提交回复
热议问题