27

In Android, if you want to clear your current Activity stack and launch a new Activity (for example, logging out of the app and launching a log in Activity), there appears to be two approaches.

Are there any advantages to one over the other if your target API level is above 16?

1) Finish Affinity

Calling finishAffinity() from an Activity. Activity.finishAffinity

2) Intent Flags

Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

The finishAffinity() approach is suitable for >= API 16.

The Intent flags approach is suitable for >= API 11.

To be clear, for the purpose of clearing the current Activity stack, both approaches appear to work equally as well. My question is are there are problems with either that people have experienced and, therefore, is there any reason to choose one over the other?

Craig Russell
  • 3,534
  • 3
  • 26
  • 27

4 Answers4

16

Functionally, there's no difference, but testing this out on GenyMotion there appears to be a slight visual difference. See web cast: https://drive.google.com/file/d/0B8Y77sY7Y2CGRS02c3UyNjd2MGs/view?usp=sharing

You would need to try that on a range of devices to see how consistent it is.

Subjectively, I would say go with the finishAffinity() because it's more explicit. However, if you have to support < SDK 16 you don't really have a choice.

brindy
  • 4,585
  • 2
  • 24
  • 27
  • The delay, or flicker, that you mention is initially what made me question finishAffinity(). IIRC, some Samsung devices in particular had a very pronounced flicker when using finishAffinity() but I no longer have the devices to test. – Craig Russell Nov 04 '15 at 09:30
  • In this case it was the intent version that had the flicker as per the video. – brindy Nov 04 '15 at 09:46
  • Actually, I'm not sure I'd call it a flicker (based on the vide). There's something definitely different about what happens visually though. finishAffinity looks like it appears on top, the intent version looks like it has "finished" revealing the other one on the top of the stack. – brindy Nov 04 '15 at 12:31
  • You have a choice using ActivityCompat. – Eury Pérez Beltré Sep 27 '17 at 03:11
  • 5
    @brindy Too bad the video is not available anymore. – Manuel Jan 20 '19 at 15:28
0

You should use intent flags for that.

What if you have a large stack of activities, will you call from each one to finish them all?

Its much better and easier to just call a Intent.

Hope this helps.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • The purpose of both approaches listed in my question is to finish all activities in the current stack without having to call finish() on each one individually. To be clear, as far as I can see, both approaches accomplish this. – Craig Russell Nov 04 '15 at 08:24
0

Try this

Intent.FLAG_ACTIVITY_CLEAR_TOP

it clears the stack of previous activities

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

If API >= 21, you can use the command of:

finishAndRemoveTask ();

Finishes all activities in this task and removes it from the recent tasks list.

https://developer.android.com/reference/android/app/ActivityManager.AppTask.html

Mike Yan
  • 1,669
  • 2
  • 21
  • 27