我正在開發一個應用程序,該應用程序顯示與繪製卡片有關的規則。用戶可以在SettingsActivity
中設置自定義規則,並將其加載到GameActivity
中。SharedPreferences在更改時未更新,僅在應用程序重置時更新?
但是,如果用戶決定更改其規則或重置爲默認值,則在GameActivity
中更新的唯一方法是完全退出應用程序並重新啓動。我試過使用,但它沒有奏效。當我使用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
的變化?