简体   繁体   English

按下后退按钮时如何关闭android app

[英]how to close android app when back button pressed

I am building a simple login application on android platform. 我正在android平台上构建一个简单的登录应用程序。

The 1st Activity is a Login Activity and the 2nd one is a welcome screen. 第一个活动是登录活动,第二个活动是欢迎屏幕。
When a user presses the login button, I start the 2nd Activity by using an Intent. 当用户按下登录按钮时,我将使用Intent启动第二个活动。
But in the 2nd Activity, when the user presses the back button, it opens the 1st Activity. 但是在第二活动中,当用户按下后退按钮时,它将打开第一活动。
But at this point I want to close my app. 但是在这一点上,我想关闭我的应用程序。

Can I use Fragments? 我可以使用片段吗?
Any other alternative? 还有其他选择吗?

you have to just finish your activity when you switching another activity, 您只需在切换其他活动时完成活动,

Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();

you call the finish() in LoginActivity when you start the your SecondActivity 调用finish()LoginActivity当您启动您的SecondActivity

Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();

OR 要么

you set the android:noHistory = "true" in LoginActivity in AndroidManifest.xml. LoginActivity在AndroidManifest.xml的LoginActivity中设置android:noHistory = "true"

you need to create your second activity with FLAG_ACTIVITY_CLEAR_TOP from your first activity. 您需要从第一个活动中使用FLAG_ACTIVITY_CLEAR_TOP创建第二个活动。

and then in your second activity you need to call finish() in onbackpressed() 然后在第二个活动中,您需要在onbackpressed()调用finish() onbackpressed()

@Override
public void onBackPressed() {
    finish();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM