简体   繁体   中英

Preferences not saving and variables cleared when launching activity

On my MainActivity I have a counter tracking Wins, Draws and Losses, after clicking a button, another activity is launched and when that activity is finished, the user is asked whether they won, drew or lost. I'm trying to get the counters on my main activity to update but the preferences don't seem to be storing correctly, after finishing the 2nd activity and returning to the first, the counter is updated but when going to the 2nd activity again, the counters are cleared and revert to zero. The preferences don't appear to be saving either, despite using it successfully previously.

Debugging hasn't come up with anything, I'd appreciate any input!

From MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    new FlurryAgent.Builder().withLogEnabled(true).build(this, "");

    setContentView(R.layout.activity_main);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MainPref", MODE_PRIVATE);
    final SharedPreferences.Editor editor = pref.edit();

    editor.putInt("wins", numberOfWins);
    editor.putInt("draws", numberOfDraws);
    editor.putInt("losses", numberOfLosses);

    numberOfWins = pref.getInt("wins", 0);
    numberOfDraws = pref.getInt("draws", 0);
    numberOfLosses = pref.getInt("losses", 0);

    TextView wins = findViewById(R.id.numberOfWins);
    wins.setText(String.valueOf(numberOfWins));

    TextView draws = findViewById(R.id.numberOfDraws);
    draws.setText(String.valueOf(numberOfDraws));

    TextView losses = findViewById(R.id.numberOfLosses);
    losses.setText(String.valueOf(numberOfLosses));

    editor.commit();

From 2nd activity:

final ImageButton completeBattle = findViewById(R.id.completeBattle);
    completeBattle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(BattleActivity.this);
            dialog.setContentView(R.layout.custom_dialog);

            dialog.show();

            Button cancelBtn = dialog.findViewById(R.id.cancelBtn);
            cancelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    dialog.dismiss();

                }
            });

            Button winBtn = dialog.findViewById(R.id.winBtn);
            winBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    MainActivity.numberOfWins++;

                    editor.clear();
                    editor.commit();



                    dialog.dismiss();

                    Intent intent = new Intent(BattleActivity.this, MainActivity.class);
                    startActivity(intent);

                }
            });

            Button drawBtn = dialog.findViewById(R.id.drawBtn);
            drawBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    MainActivity.numberOfDraws++;

                    editor.clear();
                    editor.commit();



                    dialog.dismiss();

                    Intent intent = new Intent(BattleActivity.this, MainActivity.class);
                    startActivity(intent);

                }
            });

            Button loseBtn = dialog.findViewById(R.id.loseBtn);
            loseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    MainActivity.numberOfLosses++;

                    editor.clear();
                    editor.commit();




                    dialog.dismiss();

                    Intent intent = new Intent(BattleActivity.this, MainActivity.class);
                    startActivity(intent);

                }
            });

May be worth mentioning I have another set of preferences running on the 2nd activity which is used to track a different set of counters and when the user completes a game, these counters are reset.

When you start an Activity it is a new instance and doesn't know about previous instances of that Activity or the state of them. You need to pass into the Activity any state data it needs to start up with from the main activity, you can use the Intent's extra data to pass data to the new activity. Similarly, when the second Activity finishes, it needs to pass data back to the first activity using startActivityForResult.

Basically, your main Activity needs to be the place storing all your state data that persists and then pass and receive that data to your sub-activities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM