可能重复:
I'm working on having a "Keep me on Logged in" state on my app. How should i do it?
我得到了“是”按钮,就像用户登录并在打开应用程序时关闭应用程序一样,用户直接进入主页面。我的问题是,如果用户选择不保持其帐户登录,我希望活动在用户再次打开应用程序时再次从登录页面开始
答案 0 :(得分:0)
登录活动设置为应用程序起始点的主要活动现在第一次登录时可以将此设置设置为SharedPreference,就像我设置yes然后在SharedPreference中设置keeplogin作为键,它的值为true并且每次都检查当他启动应用程序时,首先使用SharedPreference中的密钥获取此值,如果为true,则启动另一个活动,否则继续使用此登录屏幕
这就是例如。
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = preferences.edit();
这里第一次检查未找到此密钥且条件错误并继续使用logi屏幕。现在,如果设置为true,则此条件始终为true,并重定向到另一个活动。如果设置为false,则始终以登录屏幕
开头if(preferences.getBoolean("KeepLogin", "")){
intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
finish();
}
/////// when click on the login button set the value of your radio button into the login button click event
boolean keeplogin // fetch your radio button status here and set to this object
editor.putBoolean("KeepLogin", keeplogin);
editor.commit();