如何显示登录屏幕只有一次? [英] How to display login screen only one time?

查看:185
本文介绍了如何显示登录屏幕只有一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个项目,我有一个登录界面,这是用于用户登录到
应用。这个登录屏幕应该只看到第一次,因此用户可以填写并登录,但是当用户在第二次打开应用程序,应用程序必须显示main.activity。如何使用共享preference
 我不知道如何做到这一点......

I am creating a project where I have a login screen, which is used for user to login into the
Application. This login screen should only be visible the first time, so the user can fill it and log in, but when user opens the application at the second time the application must show main.activity. How to use Shared preference.
I don't understand how to do this....

给我任何引用。
在此先感谢。

Give me any reference.
Thanks in Advance.

推荐答案

要与实现这种共享preferences 你可能会做这样的事情:

To achieve this with SharedPreferences you might do something like this:

将在下面的任何类你看更适合。让我们假设你在课堂上插入这个示例

Insert the following in any Class you see more fit. Let's suppose you insert this in class Example.

//Give your SharedPreferences file a name and save it to a static variable
public static final String PREFS_NAME = "MyPrefsFile";

现在,在评估是否succefully登录用户的方法,做到以下几点。注意示例类,则必须更改为匹配您的code。

Now, in the method that evaluates if the user succefully logged in, do the following. Notice the Example class, you must change this to match your code.

//User has successfully logged in, save this information
// We need an Editor object to make preference changes.
SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();

//Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);

// Commit the edits!
editor.commit();

最后,当你的应用程序启动:您现在可以评估,如果用户已经登录或没有。不过注意示例类,你必须改变。

Finally, when your application starts you can now evaluate if the user has already logged in or not. Still notice the Example class that you must change.

SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0);
//Get "hasLoggedIn" value. If the value doesn't exist yet false is returned
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);

if(hasLoggedIn)
{
    //Go directly to main activity.
}

希望这有助于

Hope this helps

修改开始一个新后的活性。

To prevent the user from using the back button to go back to the Login activity you have to finish() the activity after starting a new one.

继<一个采取code href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/Forwarding.html">Forwarding.java | Android开发

// Here we start the next activity, and then call finish()
// so that our own will stop running and be removed from the
// history stack
Intent intent = new Intent();
intent.setClass(Forwarding.this, ForwardTarget.class);
startActivity(intent);
Example.this.finish();

所以,你必须做你的code是调用完成()在登录活动功能,调用后 startActivity()

So, what you have to do in your code is to call the finish() function on the Login activity, after calling startActivity().

另请参见:<一href="http://stackoverflow.com/questions/1898886/removing-an-activity-from-the-history-stack">Removing从历史堆栈

这篇关于如何显示登录屏幕只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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