How to exit from the application and show the home screen?

后端 未结 20 2040
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:45

I have an application where on the home page I have buttons for navigation through the application.

On that page I have a button \"EXIT\" which when clicked should t

20条回答
  •  伪装坚强ぢ
    2020-11-22 10:27

    This works well for me.
    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
        }
    

提交回复
热议问题