Quitting application in Android

前端 未结 3 1198
孤城傲影
孤城傲影 2021-01-14 13:22

I want to quit application in Android. Just put a \"quit\" button, which kills my app.

I know I shouldn\'t do this. I know that this is not the philosophy of the OS.

3条回答
  •  长发绾君心
    2021-01-14 13:47

    Close all the previous activities as follows:

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("Exit me", true);
        startActivity(intent);
        finish();
    

    Then in MainActivity onCreate() method add this to finish the MainActivity

        setContentView(R.layout.main_layout);
    
        if( getIntent().getBooleanExtra("Exit me", false)){
            finish();
            return; // add this to prevent from doing unnecessary stuffs
        }
    

提交回复
热议问题