[英]Finish previous activities on button clicked android
My android app has 3 screen. 我的Android应用有3个屏幕。
Screen 1 starts Screen 2 and screen 2 starts screen 3. 屏幕1启动屏幕2,屏幕2启动屏幕3。
I have added a button named EXIT on screen 3. What I want is when user press this button, Screen 1 and Screen 2 activities should get finished. 我在屏幕3上添加了一个名为EXIT的按钮。我想要的是当用户按下此按钮时,屏幕1和屏幕2的活动应该完成。
When you press the Exit button in your screen 3 you might start another activity which will kill all other activities in the stack and will close itself. 当您按屏幕3上的退出按钮时,您可能会启动另一个活动,该活动将杀死堆栈中的所有其他活动并自行关闭。 Here's my working sample to do the trick.
这是我完成该工作的示例。
public class ExitActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT >= 21) {
finishAndRemoveTask();
} else {
finish();
}
}
public static void exitApplication(Context context) {
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
}
}
So you need to call exitApplication
function in your Exit button onClick
. 因此,您需要在退出按钮
onClick
调用exitApplication
函数。
on exit button click write the following code: 在退出按钮上,单击以编写以下代码:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the previous activities. 这将清除之前的所有活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.