How to close Android application?

后端 未结 22 1692
小蘑菇
小蘑菇 2020-11-22 07:17

I want to close my application, so that it no longer runs in the background.

How to do that? Is this good practice on Android platform?

If I rely on the \"ba

22条回答
  •  生来不讨喜
    2020-11-22 08:11

    Just write this code on your button EXIT click.

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("LOGOUT", true);
    startActivity(intent);
    

    And in the onCreate() method of your MainActivity.class write below code as a first line,

    if (getIntent().getBooleanExtra("LOGOUT", false))
    {
        finish();
    }
    

提交回复
热议问题