How to close Android application?

后端 未结 22 1516
小蘑菇
小蘑菇 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:07

    none of all above answers working good on my app

    here is my working code

    on your exit button:

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    ComponentName cn = intent.getComponent();
    Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
    mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    mainIntent.putExtra("close", true);
    startActivity(mainIntent);
    finish();
    

    that code is to close any other activity and bring MainActivity on top now on your MainActivity:

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

提交回复
热议问题