【发布时间】:2017-10-04 22:42:34
【问题描述】:
在我的应用程序中,我实现了密码屏幕,但需要运行检查以查看是否应显示 Activity A 或 Activity B,并且此检查显然是共享首选项的结果,但是,打开时有明显的延迟应用程序 - 代码如下所示,但我如何优化它以使其更快地打开且没有延迟?
启动器活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean loggedIn = PreferenceUtils.getBoolean(LauncherActivity.this, PreferenceUtils.LOGGED_IN_STATE);
boolean passwordEnabled = PreferenceUtils.getBoolean(LauncherActivity.this, PreferenceUtils.APPLICATION_PASSWORD_ENABLED);
Log.d(TAG, "LOGGED IN: " + loggedIn + " " + "PW: " + passwordEnabled);
if (!loggedIn) {
// Start Login Activity
Intent intent = new Intent(LauncherActivity.this, ApplicationIntro.class);
startActivity(intent);
//PreferenceUtils.clearPreferences(LauncherActivity.this);
} else {
if (passwordEnabled) {
// Start Password Activity
Intent intent = new Intent(LauncherActivity.this, PasswordActivity.class);
startActivity(intent);
} else {
// Start Main Activity
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(intent);
}
}
finish();
}
密码活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
setupActionBar();
setupPreferences();
findViews();
setupValues();
setListeners();
}
...
private void setupPreferences() {
attemptsRemaining = PreferenceUtils.getInt(PasswordActivity.this, PreferenceUtils.APPLICATION_PASSWORD_ATTEMPTS);
storedPassword = PreferenceUtils.getString(PasswordActivity.this, PreferenceUtils.APPLICATION_PASSWORD);
duressPassword = PreferenceUtils.getString(PasswordActivity.this, PreferenceUtils.DURESS_PASSWORD);
}
偏好工具
public static void saveString(Context context, String key, String value) {
SharedPreferences preferences = new SecurePreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String getString(Context context, String key) {
SharedPreferences preferences = new SecurePreferences(context);
return preferences.getString(key, "");
}
public static void saveBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = new SecurePreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBoolean(Context context, String key) {
SharedPreferences preferences = new SecurePreferences(context);
return preferences.getBoolean(key, false);
}
【问题讨论】:
标签: java android android-intent android-activity android-sharedpreferences