Android 检查之前登录过的用户,否则开始登录活动 [英] Android check user logged in before, else start login activity
问题描述
我希望在用户启动应用程序但之前未登录时启动登录活动.如果之前已成功登录,应用程序将跳过登录页面并移至 MainMenu.java.我现在拥有的是:
public class Login extends Activity 实现 OnClickListener, TaskCompleteCallback{first_time_check();...@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.configure);...}私人布尔 first_time_check() {String first = mPreferences.getString("first", null);if((第一个==空)){Intent i = new Intent(Login.this, MainMenu.class);开始活动(i);}返回假;}...SharedPreferences.Editor 编辑器 = mPreferences.edit();editor.putString("first", value);...editor.commit();//关闭活动Intent i = new Intent(Login.this, MainMenu.class);开始活动(i);}
但我得到了 FC.我实现 SharedPreferences 的方式有问题吗?
您的代码只是从不调用 first_time_check()
,因此在返回用户的情况下自动转发不起作用.>
你可以在 onCreate()
中做
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);first_time_check();setContentView(R.layout.configure);...}
因此对于新用户,first_time_check()
会将他转发到登录页面,否则将显示当前布局,他可以在此页面上继续.
I want the login activity to start when the user starts the app but has not logged in before. If a successful login has been completed before, the app will skip the login page and move to MainMenu.java. What I have now is:
public class Login extends Activity implements OnClickListener, TaskCompleteCallback{
first_time_check();
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configure);
...}
private boolean first_time_check() {
String first = mPreferences.getString("first", null);
if((first == null)){
Intent i = new Intent(Login.this, MainMenu.class);
startActivity(i);
}
return false;
}
...
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString("first", value);
...
editor.commit();
// Close the activity
Intent i = new Intent(Login.this, MainMenu.class);
startActivity(i);
}
But I get FCs'. Is something wrong with how I implemented SharedPreferences?
Your code just never calls that first_time_check()
, thus the automatic forward in case of a returning user does not work.
You could in onCreate()
do
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
first_time_check();
setContentView(R.layout.configure);
...}
So for a new user, first_time_check()
would forward him to the login page, otherwise
the current layout would be shown and he could continue on this page.
这篇关于Android 检查之前登录过的用户,否则开始登录活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!