1

私には2つの活動があります。1 つはHomeScreenで、もう 1 つはGridViewです。アプリは、以下のようにSharedPreferencesを持つ HomeScreen アクティビティで開きます。

再生ボタンを選択し、 GridViewである次のアクティビティに移動してゲームをプレイした後、 SharedPreferencesにスコアを追加する必要がありますが、戻るボタンを押してHomeScreenアクティビティに戻ると、スコアが表示されないようです。強制的に閉じて再度開かない限り、更新されません。onResume()メソッドを呼び出すと、スコアが 1 回ではなく 2 倍になります。

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    addScores();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_screen);

    play = (Button) findViewById(R.id.play_game);
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showMovieDialog();
        }
    });
}

public void addScores(){
    final SharedPreferences app_preferences =
        PreferenceManager.getDefaultSharedPreferences(this);
    totalCoins = app_preferences.getInt("Scores: ",0);
    SharedPreferences.Editor editor = app_preferences.edit();
    totalCoins = totalCoins + Bollywood.coins;

    tCoins = (TextView)findViewById(R.id.totalCoins);
    tCoins.setText("" + totalCoins);

    editor.putInt("Scores: ", totalCoins);
    editor.commit();
}
4

1 に答える 1