我有一个应用程序,我已经选择登录Facebook。用户登录后,我在共享首选项中存储一个布尔值,以便下次用户打开应用程序时,他将被定向到主屏幕而不是登录屏幕。
我注意到一件事,如果我杀了应用程序,它会在下次打开时要求登录。它是Android应用程序的默认行为还是可以修复,以便我的应用程序即使在它被杀死后仍保留登录参数?
我正在使用TinyDB来存储和检索SharedPreferences中的值。
这是我的onCreate
方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tinyDB = new TinyDB(this);
final boolean loggedIn = tinyDB.getBoolean("Login", false);
if(loggedIn) {
Intent intent = new Intent(LaunchActivity.this, HomeActivity.class);
startActivity(intent);
LaunchActivity.this.finish();
}
Fabric.with(this, new Crashlytics());
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_launch);
}
int SPLASH_TIME_OUT = 3000;
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
startLoginOrHome(loggedIn);
}
}, SPLASH_TIME_OUT);
}
private void startLoginOrHome(boolean loggedIn) {
if(loggedIn) {
LaunchActivity.this.finish();
} else {
Intent intent = new Intent(LaunchActivity.this, SignInActivity.class);
startActivity(intent);
LaunchActivity.this.finish();
}
}
答案 0 :(得分:0)
对我来说,你的问题在于保存布尔值。 无需使用TinyDB只需在登录时添加以下代码即可。
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.edit().putBoolean("login", true).commit();
并在OnCreate中使用
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
Boolean isLoggedIn = prefs.getBoolean("login", false);
Log.v(TAG,String.ValueOf(isLoggedIn)); //Use Logging to monitor
if(isLoggedIn) {
goToHomeScreen()
}
else
goToLogInScreen()