7

I would terminate my app and cancel it from the list of recent task.

finishAndRemoveTask() is available only on API 21.

What should I use on API lower than 21??

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
user3253955
  • 433
  • 1
  • 8
  • 17

2 Answers2

3

Make an intent to the first activity in the stack and finish the current activity:

Intent intent  = new Intent(this, FirstActivity.class);
intent.putExtra(EXTRA_FINISH, true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
startActivity(intent);
finish();

And, in the onResume method of the FirstActivity, something like this to finish the last activity in the stack (and hopefully removing the app from the recent apps list):

if (getExtras() != null && getIntentExtra(EXTRA_FINISH, false)) {
   finish();
}
thelawnmowerman
  • 11,956
  • 2
  • 23
  • 36
  • This would work in a very specific use case where `FirstActivity` is the only activity in the stack and it's the only instance of that activity in that task and it would not remove the task from recent tasks. In other words that does not have a similar effect as `finishAndRemoveTask` – Alex.F Dec 11 '16 at 17:32
  • I agree, it's a very specific use case, but fortunately it's the most common one, too. There are many more simple apps with a single launcher/menu activity than more complex flows. Correction: this do have a *similar* effect as `finishAndRemoveTask`, not the *same one*, of course (and, as you can see, no other suggestions have been made, so it's the best we have for now). The rest of your analysis is mistaken: many activities can be in stack and even many instances of them, thanks to the FLAG_ACTIVITY_CLEAR_TOP flag. Anyway and most important: this is just a workaround for legacy devices. – thelawnmowerman Dec 12 '16 at 13:15
  • does't finish just close the current activity. in this case First activity. – Vikas Pandey May 19 '17 at 07:25
0

I had a similar use case where I needed to finish all activities. Here is one way to do it without finishAndRemoveTask().

Make all your activities extend a base class with the following things in it:

private Boolean mHasParent = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mHasParent = extras.getBoolean("hasParent", false);
    }
}

// Always start next activity by calling this.
protected void startNextActivity(Intent intent) {
    intent.putExtra("hasParent", true);
    startActivityForResult(intent, 199);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);  
    if (requestCode == 199 && resultCode == FINISH_ALL) {
        finishAllActivities();
    }
}

protected void finishAllActivities() {
    if (mHasParent) {
        // Return to parent activity.
        setResult(FINISH_ALL);
    } else {
        // This is the only activity remaining on the stack.
        // If you need to actually return some result, do it here.
        Intent resultValue = new Intent();
        resultValue.putExtra(...);
        setResult(RESULT_OK, resultValue);
    }

    finish();
}

Simply call finishAllActivities() in any activity, and all the activities will unwind. Ofcourse if you don't care what result the last activity returns, the code can be made much simpler.

Piittis
  • 318
  • 2
  • 7