Android 中的 SharedPreferences 未按预期工作
SharedPreferences not working as expected in Android
我的应用程序中有一个帮助 activity,我希望它仅在第一个 运行 时启动。
我试过这个:
在创建帮助 Activity 中:
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", true);
editor.commit();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
在帮助的 onResume 中 Activity:
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (!firstRun) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
} else {
Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
}
}
在 Main 的 onCreate 中Activity:
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
boolean firstRun = settings.getBoolean("firstRun", true);
Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());
但它不显示帮助 activity,当它 运行 应用程序时,它只是跳转到 MainActivity
!!
我的应用程序中有一个退出按钮,当我想使用该按钮退出应用程序时,它会再次显示 MainActivity
并且没有退出应用程序。
在 MainActivity
的 onCreate 中执行此操作。如果您将 MainActivity
设置为启动器 activity,那么这就是您所需要的。这是我会推荐的。
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean firstRun = settings.getBoolean("firstRun", true);
if (firstRun) {
settings.edit().putBoolean("firstRun", false).apply();
//start help activity
}
使用 MainActivity
作为启动器 activity 您应该可以更快地启动,因为您不会每次都创建两个活动。而且你避免在后台堆栈中有两个活动。
ps:Google 不推荐使用 "exit" 按钮。相反,您应该依靠 back\home 按钮关闭应用程序并让 OS 决定何时销毁应用程序。
Is quitting an application frowned upon?
我的应用程序中有一个帮助 activity,我希望它仅在第一个 运行 时启动。
我试过这个:
在创建帮助 Activity 中:
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", true);
editor.commit();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
在帮助的 onResume 中 Activity:
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (!firstRun) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
} else {
Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
}
}
在 Main 的 onCreate 中Activity:
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
boolean firstRun = settings.getBoolean("firstRun", true);
Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());
但它不显示帮助 activity,当它 运行 应用程序时,它只是跳转到 MainActivity
!!
我的应用程序中有一个退出按钮,当我想使用该按钮退出应用程序时,它会再次显示 MainActivity
并且没有退出应用程序。
在 MainActivity
的 onCreate 中执行此操作。如果您将 MainActivity
设置为启动器 activity,那么这就是您所需要的。这是我会推荐的。
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean firstRun = settings.getBoolean("firstRun", true);
if (firstRun) {
settings.edit().putBoolean("firstRun", false).apply();
//start help activity
}
使用 MainActivity
作为启动器 activity 您应该可以更快地启动,因为您不会每次都创建两个活动。而且你避免在后台堆栈中有两个活动。
ps:Google 不推荐使用 "exit" 按钮。相反,您应该依靠 back\home 按钮关闭应用程序并让 OS 决定何时销毁应用程序。
Is quitting an application frowned upon?