本文介绍了Activity.finishAffinity()与Intent.FLAG_ACTIVITY_NEW_TASK |意图.FLAG_ACTIVITY_CLEAR_TASK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,如果要清除当前的Activity堆栈并启动新的Activity(例如,注销应用程序并在Activity中启动日志),则似乎有两种方法.

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.

如果您的目标API级别高于16,那么彼此之间是否有其他优势?

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

1)完成关联性

从活动中调用finishAffinity(). Activity.finishAffinity

2)意图标志

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

finishAffinity()方法适用于> = API 16.

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

Intent标志方法适用于> = API 11.

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

为清楚起见,出于清除当前Activity堆栈的目的,两种方法似乎同样有效.我的问题是,人们经历过这两个问题是否存在问题,因此,是否有任何理由选择一个而不是另一个?

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?

推荐答案

从功能上讲,没有区别,但在GenyMotion上进行测试似乎在视觉上有细微的区别.查看网络广播: https://drive.google.com/file/d /0B8Y77sY7Y2CGRS02c3UyNjd2MGs/view?usp = sharing

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.

主观上,我想说finishAffinity(),因为它更明确.但是,如果您必须支持<您真的没有选择的SDK 16.

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.

这篇关于Activity.finishAffinity()与Intent.FLAG_ACTIVITY_NEW_TASK |意图.FLAG_ACTIVITY_CLEAR_TASK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 22:17