我正在构建一个 Android 应用程序,我希望用户通过他们的 Facebook 详细信息进行连接。根据我的设计,当应用程序第一次启动时,我想显示一个带有 LOGIN facebook 按钮的布局。在用户第一次执行登录后,我不想再次显示此布局/活动 - 当应用程序重新启动时,
我想显示另一个(主)屏幕,而不是登录屏幕。
我应该如何实现这个功能?
您需要跟踪用户是否已登录。完成此操作的最简单方法可能是使用SharedPreferences,尽管有很多不同的方法可以完成此操作。
一些伪代码可能最能说明如何做到这一点:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (!SharedPreferences.getBoolean("isLoggedIn")){
// 1. User is not logged in, show login layout
setContentView(R.layout.login);
// 2. Let user login to Facebook
// 3. If login successful:
SharedPreferences.putBoolean("isLoggedIn", true);
Intent intent = new Intent(this, HomeActivity.class);
startHomeActivity();
// 4. ..else, show error message.
} else {
// isLoggedIn was true, so user is logged in. Start HomeActivity
startHomeActivity()
}
}
public void startHomeActivity(){
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
}