一次登录 [英] One time Sign-in

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

问题描述

我正在创建一个项目,我有一个登录屏幕,用于登录
应用程序。此登录屏幕应该只在第一次可见,因此用户可以填写并登录,但是当用户第二次打开应用程序时,应用程序必须显示main.activity。如何使用共享首选项执行此操作?

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 preferences to do this?

推荐答案

在SharedPeference中保存用户的登录信息:

Save the login information of the user in SharedPeference:

SharedPreferences preferences = getSharedPreferences("preference",MODE_PRIVATE);
preferences.edit().putBoolean("LoggedIn", true).apply();

当用户注销时,将布尔LoggedIn保存为false:

And save the boolean "LoggedIn" to false when the User logs out :

preferences.edit().putBoolean("LoggedIn", false).apply();

在SplashActivity中,从sharedprefence获取值并调用相应的活动:

In the SplashActivity get the value from sharedprefence and call respective activities:

boolean loggedIn =  preferences.getBoolean("LoggedIn", false);

if(loggedIn){
// call main activity
}else{
//call login activity
}

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

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