Sha*_*ank 4 android sharedpreferences
我有一个活动,我只想在第一次运行应用程序时运行.我检查了一个返回布尔值的指定共享首选项.如果它返回true,它将被启动并且它将被设置为false,以便下次打开应用程序时它不会运行.但我的实施出错了.每次打开BeforMain1活动都会被打开.有人可以告诉我我的代码有什么问题吗?
sharedPreferences = getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
boolean firstTime=sharedPreferences.getBoolean("first", true);
if(firstTime) {
editor.putBoolean("first",false);
Intent intent = new Intent(SplashScreen.this, BeforeMain1.class);
startActivity(intent);
}
else
{
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
您忘记提交SharedPreferences更改,
if(firstTime) {
editor.putBoolean("first",false);
//For commit the changes, Use either editor.commit(); or editor.apply();.
editor.commit(); or editor.apply();
Intent intent = new Intent(SplashScreen.this, BeforeMain1.class);
startActivity(intent);
}else {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6341 次 |
| 最近记录: |