我正在开发一个应用程序,显示与正在绘制的卡相关的规则。用户可以在SettingsActivity
中设置自定义规则,并将其加载到GameActivity
中。
这没有问题,但是,如果用户决定更改其规则或重置为默认值,则GameActivity
中更新此选项的唯一方法是完全退出应用程序并重新启动。我尝试过使用reCreate()
,但它没有用。当我使用finish()
时,它就完全退出了应用程序。
在GameActivity
中,我也尝试在onResume()
方法中调用loadRules方法,但它没有做任何事情。
从SettingsActivity剪下的代码:
public void setDefault(View v){
ruleAce = rules[0];
ruleTwo = rules[1];
ruleThree = rules[2];
ruleFour = rules[3];
ruleFive = rules[4];
ruleSix = rules[5];
ruleSeven = rules[6];
ruleEight = rules[7];
ruleNine = rules[8];
ruleTen = rules[9];
ruleJack = rules[10];
ruleQueen = rules[11];
ruleKing = rules[12];
saveRules();
}
private void saveRules(){
SharedPreferences ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king;
SharedPreferences.Editor editor;
ace = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = ace.edit();
editor.putString(RULE_ACE, ruleAce);
editor.apply();
two = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = two.edit();
editor.putString(RULE_TWO, ruleTwo);
editor.apply();
three = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = three.edit();
editor.putString(RULE_THREE, ruleThree);
editor.apply();
four = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = four.edit();
editor.putString(RULE_FOUR, ruleFour);
editor.apply();
five = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = five.edit();
editor.putString(RULE_FIVE, ruleFive);
editor.apply();
six = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = six.edit();
editor.putString(RULE_SIX, ruleSix);
editor.apply();
seven = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = seven.edit();
editor.putString(RULE_SEVEN, ruleSeven);
editor.apply();
eight = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = eight.edit();
editor.putString(RULE_EIGHT, ruleEight);
editor.apply();
nine = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = nine.edit();
editor.putString(RULE_NINE, ruleNine);
editor.apply();
ten = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = ten.edit();
editor.putString(RULE_TEN, ruleTen);
editor.apply();
jack = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = jack.edit();
editor.putString(RULE_JACK, ruleJack);
editor.apply();
queen = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = queen.edit();
editor.putString(RULE_QUEEN, ruleQueen);
editor.apply();
king = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
editor = king.edit();
editor.putString(RULE_KING, ruleKing);
editor.apply();
}
GameActivity代码段:
public class kingGame extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_king_game);
loadRules();
shuffleDeck();
}
@Override
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_king_game);
loadRules();
}
//Sets the Rules
static String aceRule, twoRule, threeRule, fourRule, fiveRule, sixRule, sevenRule, eightRule, nineRule, tenRule, jackRule, queenRule, kingRule;
static String[] defaultrules = {DEFAULT RULES IN HERE}
public void loadRules(){
SharedPreferences settings;
settings = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);
aceRule = settings.getString(RULE_ACE, defaultrules[0]);
twoRule = settings.getString(RULE_TWO, defaultrules[1]);
threeRule = settings.getString(RULE_THREE, defaultrules[2]);
fourRule = settings.getString(RULE_FOUR, defaultrules[3]);
fiveRule = settings.getString(RULE_FIVE, defaultrules[4]);
sixRule = settings.getString(RULE_SIX, defaultrules[5]);
sevenRule = settings.getString(RULE_SEVEN, defaultrules[6]);
eightRule = settings.getString(RULE_EIGHT, defaultrules[7]);
nineRule = settings.getString(RULE_NINE, defaultrules[8]);
tenRule = settings.getString(RULE_TEN, defaultrules[9]);
jackRule = settings.getString(RULE_JACK, defaultrules[10]);
queenRule = settings.getString(RULE_QUEEN, defaultrules[11]);
kingRule = settings.getString(RULE_KING, defaultrules[12]);
}
/* Creates the Deck */
/* Shuffles the Deck */
public void shuffleDeck(){
cardsLeft = 52;
cardNum = 0;
for (int i = 0; i < RANK.length; i++){
for (int j=0; j < SUITS.length; j++){
deck[SUITS.length*i + j] = RANK[i] + " Of " + SUITS[j];
}
}
for (int i = 0; i < n; i++){
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
}
/* Button Press Draws the Card */
public void drawCard(View view) {}
之后,它只是printCard()
,showRule()
和newGame()
的方法。
只有在游戏结束时(到达牌组的末尾)才会调用newGame。
showRule只显示加载到aceRule,twoRule等中的规则,以响应绘制的卡片。
printCard显示抽出的卡片。
所以我的问题是,当活动恢复时,如何强制GameActivity
检查SharedPreferences
中的更改?
答案 0 :(得分:0)
在onPause方法中,您可以使用.edit()。putX()。apply()对共享的prefs对象保存值