Android-按下后退按钮时如何退出活动? [英] Android - how to exit activity when back button is pressed?

查看:109
本文介绍了Android-按下后退按钮时如何退出活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Android按下后退按钮应退出该应用

Possible Duplicate:
android pressing back button should exit the app

我正在建立一个注册和登录屏幕,仅用于练习.因此,我放置了SharedPreferences,以便当用户再次打开应用程序时,它将引导他们进入登录活动,而不是从注册重新开始.但是,当我从登录活动中按返回按钮时,仍然需要我重新注册活动,这可能会更改已保存的SharedPreferences.我想禁用此功能,如何在用户按下后退按钮时简单地退出应用程序?

I'm building a register and login screen just for an exercise. So I put SharedPreferences so that when user open the app again, it will direct them to login activity and not start again from register. However, when I press back button from login activity it still take me back to register activity which could change the already saved SharedPreferences. I wanna disable this function, how to simply exit the app when user press the back button?

谢谢

推荐答案

如果您从不希望后退按钮返回到注册屏幕,最干净的解决方案是将其从活动历史记录中排除使用清单中的 noHistory 属性,即

If you never want the back button to return to the registration screen, the cleanest solution would be to exclude it from the activity history using the noHistory attribute in the manifest, i.e.

    <activity
        android:name=".RegistrationActivity"
            ...
        android:noHistory="true" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

这篇关于Android-按下后退按钮时如何退出活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆