简体   繁体   English

启动活动时不保存首选项并清除变量

[英]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. 在我的MainActivity上,我有一个计数器来跟踪赢,输和输,单击一个按钮后,会启动另一个活动,当该活动结束时,询问用户他们是赢还是输还是输。 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: 从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. 您需要将其从主活动开始所需的任何状态数据传递给活动,您可以使用Intent的额外数据将数据传递给新活动。 Similarly, when the second Activity finishes, it needs to pass data back to the first activity using startActivityForResult. 同样,第二个活动完成时,需要使用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. 基本上,您的主要Activity必须是存储所有持久状态的状态数据,然后将其传递和接收到子活动的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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