Java 基于pref的加载意图
我想加载一个pref值,然后决定是在intent中加载活动a还是活动B 大概是这样的:Java 基于pref的加载意图,java,android,Java,Android,我想加载一个pref值,然后决定是在intent中加载活动a还是活动B 大概是这样的: SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean pref1 = prefs.getBoolean("pref1", true); // Pseudocode if (pref1) { class nextScreen = aActivity.class; } else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean pref1 = prefs.getBoolean("pref1", true);
// Pseudocode
if (pref1) {
class nextScreen = aActivity.class;
} else {
class nextScreen = bActivity.class;
}
Intent goToMainActivity = new Intent(this, nextScreen);
我对android和java非常陌生,所以请耐心等待。通常,要在条件之外使用
变量,您需要在进入条件之前声明它:
type variable; // declaration
if (condition) {
variable = value1; // assign a specific value
else {
variable = value2; // assign an other value
}
// use 'variable' with the value setted
因此,您将能够在条件之后的意图中使用它。然后,为了在您的案例中使用意图,您需要知道第二个元素的类型。在您提供的这个示例中,它是一个,使用的方法是public Intent(Context-packageContext,Class-cls)
因此,它应该很简单,因为:
Class nextScreen = null;
if (pref1) {
nextScreen = aActivity.class;
} else {
nextScreen = bActivity.class;
}
if (nextScreen != null) {
Intent goToNextActivity = new Intent(this, nextScreen);
}
你的问题是…?我怎么能把它存档。我的意思是“下一屏上课”不是一个有效的代码。我找了很多,但没有找到合适的答案