无法删除共享首选项中的旧数据当活动重新创建时,旧数据重新出现

798qvoo8  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(164)

我正在尝试开发一个21点游戏,当玩家第一次开始游戏时,它将开始为庄家和玩家各发2张牌。

假设在处理了前4张牌之后,玩家关闭了应用程序(完全杀死了它)->打开应用程序并点击play btn->旧游戏进度恢复。

玩了一段时间后。。游戏的胜负已经决定,所以会出现两个按钮:再次玩并退出。。
玩家点击退出按钮->返回正厅->点击播放按钮->将开始一个新游戏
玩家再次点击play->将开始一个新游戏
但是,我在清除游戏中存储的旧数据时遇到了问题。单击“再次播放”按钮时,旧的游戏进度重新出现。同样当我点击退出按钮->返回游戏大厅->点击播放按钮时,旧的游戏进度重新出现。
我的问题是,为什么会发生这种情况,我该如何解决?
另外,如果您发现与数据恢复相关的其他错误/错误实现,我很高兴您能告诉我。谢谢您。
我的oncreate()方法:

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

        //create references for the views
        mBtnStand = findViewById(R.id.btn_stand); //stand image button
        mBtnHit = findViewById(R.id.btn_hit); // hit image button
        mBtnQuit = findViewById(R.id.btn_quit);
        mBtnPlayAgain = findViewById(R.id.btn_playAgain);

        mImgPCard1 = findViewById(R.id.img_pcard1);
        mImgPCard2 = findViewById(R.id.img_pcard2);
        mImgPCard3 = findViewById(R.id.img_pcard3);
        mImgPCard4 = findViewById(R.id.img_pcard4);
        mImgPCard5 = findViewById(R.id.img_pcard5);

        mImgDCard1 = findViewById(R.id.img_dcard1);
        mImgDCard2 = findViewById(R.id.img_dcard2);
        mImgDCard3 = findViewById(R.id.img_dcard3);
        mImgDCard4 = findViewById(R.id.img_dcard4);
        mImgDCard5 = findViewById(R.id.img_dcard5);

        konfettiView = findViewById(R.id.viewKonfetti);

        cardSound = MediaPlayer.create(this, R.raw.flip_card_sound);
        yaySound = MediaPlayer.create(this, R.raw.yay_sound);
        awSound = MediaPlayer.create(this, R.raw.aww_sound);

        mTvTotal = findViewById(R.id.tv_total);
        mTvResult = findViewById(R.id.tvResult);
        mTvPlayAgain = findViewById(R.id.tv_playAgain);
        mTvQuit = findViewById(R.id.tv_quit);
        mTvHit = findViewById(R.id.tv_hit);
        mTvStand = findViewById(R.id.tv_stand);

        //check setting preference & retrieve the settings
        //SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        isHistorySetting = sharedPref.getBoolean("pref_history", true);  //use the key to access the value, "true" is default value (in case we cannot retrieve the value)
        maxNumCardSetting = sharedPref.getString("pref_max_cards", "3");

        Random r = new Random(); //create a Random object

        //TODO: TESTING
        //TODO: IMPLEMENT SHARED PREF (when user kill app, come back game progress still can be restored)
        mPreference = getSharedPreferences(spFileName, MODE_PRIVATE);

        //player total
        if (mPreference.contains(PLAYER_TOTAL_KEY)) {

            pTotal = mPreference.getInt(PLAYER_TOTAL_KEY, 0);
            isPrefPDTotal = true;

        } else {

            pTotal = 0;
            isPrefPDTotal = false;

        }

        //player total alt
        if (mPreference.contains(PLAYER_TOTAL_ALT_KEY)) {

            pTotalAlt = mPreference.getInt(PLAYER_TOTAL_ALT_KEY, 0);
            isPrefPDTotal = true;

        } else {

            pTotalAlt = 0;
            isPrefPDTotal = false;

        }

        //dealer total
        if (mPreference.contains(DEALER_TOTAL_KEY)) {

            dTotal = mPreference.getInt(DEALER_TOTAL_KEY, 0);
            isPrefPDTotal = true;

        } else {

            dTotal = 0;
            isPrefPDTotal = false;

        }

        //dealer total alt
        if (mPreference.contains(DEALER_TOTAL_ALT_KEY)) {

            dTotalAlt = mPreference.getInt(DEALER_TOTAL_ALT_KEY, 0);
            isPrefPDTotal = true;

        } else {

            dTotalAlt = 0;
            isPrefPDTotal = false;

        }

        if (isPrefPDTotal) {

            mTvTotal.setVisibility(View.VISIBLE);
            updatePlayerTotal();

            mBtnStand.setVisibility(View.VISIBLE);
            mBtnHit.setVisibility(View.VISIBLE);
            mTvStand.setVisibility(View.VISIBLE);
            mTvHit.setVisibility(View.VISIBLE);

        } else {

            mTvTotal.setVisibility(View.GONE);
            mBtnStand.setVisibility(View.GONE);
            mBtnHit.setVisibility(View.GONE);
            mTvHit.setVisibility(View.GONE);
            mTvStand.setVisibility(View.GONE);

        }

        //player hits
        if (mPreference.contains(PLAYER_NUM_HIT_KEY)) {

            mPNumHit = mPreference.getInt(PLAYER_NUM_HIT_KEY, 0);

        } else {

            mPNumHit = 0; //set number of hits by player to zero

        }

        //dealer hits
        if (mPreference.contains(DEALER_NUM_HIT_KEY)) {

            mDNumHit = mPreference.getInt(DEALER_NUM_HIT_KEY, 0);

        } else {

            mDNumHit = 0; //set number of hits by dealer to zero

        }

        //dealer first card
        if (mPreference.contains(DEALER_CARD_1_KEY)) {

            dCard1 = mPreference.getInt(DEALER_CARD_1_KEY, 0);
            restoreOldCards(mImgDCard1, dCard1);

        } else {

            dCard1 = r.nextInt(52) + 1; //produce random values for dealer cards and record them in respective variables

        }

        //dealer second card
        if (mPreference.contains(DEALER_CARD_2_KEY)) {

            dCard2 = mPreference.getInt(DEALER_CARD_2_KEY, 0);
            restoreOldCards(mImgDCard2, dCard2);

        } else {

            dCard2 = r.nextInt(52) + 1; //produce random values for dealer cards and record them in respective variables
        }

        //dealer third card
        if (mPreference.contains(DEALER_CARD_3_KEY)) {

            dCard3 = mPreference.getInt(DEALER_CARD_3_KEY, 0);
            restoreOldCards(mImgDCard3, dCard3);

        } else {

            mImgDCard3.setVisibility(View.GONE);
        }

        //dealer fourth card
        if (mPreference.contains(DEALER_CARD_4_KEY)) {

            dCard4 = mPreference.getInt(DEALER_CARD_4_KEY, 0);
            restoreOldCards(mImgDCard4, dCard4);

        } else {

            mImgDCard4.setVisibility(View.GONE);

        }

        //dealer fifth card
        if (mPreference.contains(DEALER_CARD_5_KEY)) {

            dCard5 = mPreference.getInt(DEALER_CARD_5_KEY, 0);
            restoreOldCards(mImgDCard5, dCard5);

        } else {

            mImgDCard5.setVisibility(View.GONE);

        }

        //player first card
        if (mPreference.contains(PLAYER_CARD_1_KEY)) {

            pCard1 = mPreference.getInt(PLAYER_CARD_1_KEY, 0);
            restoreOldCards(mImgPCard1, pCard1);

        } else {

            pCard1 = r.nextInt(52) + 1; //produce two values for player cards record them in respective variables
        }

        //player second card
        if (mPreference.contains(PLAYER_CARD_2_KEY)) {

            pCard2 = mPreference.getInt(PLAYER_CARD_2_KEY, 0);
            restoreOldCards(mImgPCard2, pCard2);

        } else {

            pCard2 = r.nextInt(52) + 1; //produce two values for player cards record them in respective variables

        }

        //player third card
        if (mPreference.contains(PLAYER_CARD_3_KEY)) {

            pCard3 = mPreference.getInt(PLAYER_CARD_3_KEY, 0);
            restoreOldCards(mImgPCard3, pCard3);

        } else {

            mImgPCard3.setVisibility(View.GONE);

        }

        //player fourth card
        if (mPreference.contains(PLAYER_CARD_4_KEY)) {

            pCard4 = mPreference.getInt(PLAYER_CARD_4_KEY, 0);
            restoreOldCards(mImgPCard4, pCard4);

        } else {

            mImgPCard4.setVisibility(View.GONE);
        }

        //player fifth card
        if (mPreference.contains(PLAYER_CARD_5_KEY)) {

            pCard5 = mPreference.getInt(PLAYER_CARD_5_KEY, 0);
            restoreOldCards(mImgPCard5, pCard5);

        } else {

            mImgPCard5.setVisibility(View.GONE);

        }

        mMaxCards = Integer.parseInt(maxNumCardSetting); //set maximum card (for dealer / player)

        //to track which default card (first four cards, 2 for dealer, 2 for player) is currently being produced
        int count = 1; //start at 1 (means first card of dealer)

        //hide certain views first
        mTvResult.setVisibility(View.GONE);
        mBtnPlayAgain.setVisibility(View.GONE);
        mBtnQuit.setVisibility(View.GONE);
        mTvPlayAgain.setVisibility(View.GONE);
        mTvQuit.setVisibility(View.GONE);

        animTranslate = AnimationUtils.loadAnimation(this, R.anim.translate);
        animTranslate.setDuration(500);

        animSet = AnimationUtils.loadAnimation(this, R.anim.set);
        animSet.setStartOffset(500);

        if (!isPrefPDTotal) {
            //start producing the 4 cards
            for (int i = 0; i < 4; i++) {

                if (count == 1) {

                    switch (dCard1) {//check the value of the dealer's first card, apply the image accordingly (update total of dealer as well)
                        case 1:
                            showFirstSecondFourthCard(mImgDCard1, 1000, 1500, 2000, 1);
                            isDAce1 = true; //dealer got the ace for his first card
                            //two possible values: 1 or 11
                            dTotal += 1;
                            dTotalAlt += 11;
                            break;
                        case 2:
                            showFirstSecondFourthCard(mImgDCard1, 1000, 1500, 2000, 2);
                            dTotal += 2;
                            dTotalAlt += 2;
                            break;
                        case 3:
                            showFirstSecondFourthCard(mImgDCard1, 1000, 1500, 2000, 3);
                            dTotal += 3;
                            dTotalAlt += 3;
                            break;
                        case 4:
                            showFirstSecondFourthCard(mImgDCard1, 1000, 1500, 2000, 4);
                            dTotal += 4;
                            dTotalAlt += 4;
                            break;
                        case 5:
                            showFirstSecondFourthCard(mImgDCard1, 1000, 1500, 2000, 5);
                            dTotal += 5;
                            dTotalAlt += 5;
                            break;
...other cases...
                    }

                    count++; //increment count (update the current card being produced)

                } else if (count == 2) {

                    switch (dCard2) { //check the value of the dealer's second card, update total of dealer
                        case 1:
                            isDAce2 = true;//dealer got the ace for his second card
                            if (isDAce1) {//if dealer has two ace cards (this is second ace card)
                                dTotal = 2;
                                dTotalAlt = 12;
                            } else {
                                dTotal += 1;
                                dTotalAlt += 11;
                            }
                            break;
                        case 2:
                            dTotal += 2;
                            dTotalAlt += 2;
                            break;
                        case 3:
                            dTotal += 3;
                            dTotalAlt += 3;
                            break;
                        case 4:
                            dTotal += 4;
                            dTotalAlt += 4;
                            break;

...other cases...
                    }

                    showDealerSecondImage(mImgDCard2, 2500);

                    count++; //increment count (update current card being produced)

                } else if (count == 3) {

                    switch (pCard1) { //check the value of player's first card and apply image accordingly (update player's total as well)
                        case 1:
                            showFirstSecondFourthCard(mImgPCard1, 3500, 4000, 4500, 1);
                            isPAce1 = true;//player got the ace for his first card
                            //two possible values: 1 or 11
                            pTotal += 1;
                            pTotalAlt += 11;
                            break;
                        case 2:
                            showFirstSecondFourthCard(mImgPCard1, 3500, 4000, 4500, 2);
                            pTotal += 2;
                            pTotalAlt += 2;
                            break;
                        case 3:
                            showFirstSecondFourthCard(mImgPCard1, 3500, 4000, 4500, 3);
                            pTotal += 3;
                            pTotalAlt += 3;
                            break;
                        case 4:
                            showFirstSecondFourthCard(mImgPCard1, 3500, 4000, 4500, 4);
                            pTotal += 4;
                            pTotalAlt += 4;
                            break;

...other cases
                    }

                    //update player total
                    updatePlayerTotal();

                    count++; //increment count (update current card being produced)

                } else { //set image for player's second card

                    switch (pCard2) { //check the value of player's second card and set image accordingly (update player total as well)
                        case 1:
                            showFirstSecondFourthCard(mImgPCard2, 5000, 5500, 6000, 1);
                            isPAce2 = true;//player got the ace for his second card
                            if (isPAce1) {//if player has two ace cards (this is second ace card)
                                pTotal = 2;
                                pTotalAlt = 12;
                            } else {
                                pTotal += 1;
                                pTotalAlt += 11;
                            }
                            break;
                        case 2:
                            showFirstSecondFourthCard(mImgPCard2, 5000, 5500, 6000, 2);
                            pTotal += 2;
                            pTotalAlt += 2;
                            break;
                        case 3:
                            showFirstSecondFourthCard(mImgPCard2, 5000, 5500, 6000, 3);
                            pTotal += 3;
                            pTotalAlt += 3;
                            break;
                        case 4:
                            showFirstSecondFourthCard(mImgPCard2, 5000, 5500, 6000, 4);
                            pTotal += 4;
                            pTotalAlt += 4;
                            break;

....other cases 

                    }

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            mTvTotal.setVisibility(View.VISIBLE);

                            //update player total
                            updatePlayerTotal();

                        }
                    }, 6500);

                }

            }

        }

        //CHECK IF THERE IS BLACKJACK
        if (pTotalAlt == 21) {

            //disable stand button
            mBtnStand.setVisibility(View.GONE);

            mTvStand.setVisibility(View.GONE);

            //disable hit button
            mBtnHit.setVisibility(View.GONE);

            mTvHit.setVisibility(View.GONE);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    flipDealerSecondCard();

                }
            }, 7000);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    displayWin();

                }
            }, 7500);

        } else if (dTotalAlt == 21) { //dealer wins

            //disable stand button
            mBtnStand.setVisibility(View.GONE);

            mTvStand.setVisibility(View.GONE);

            //disable hit button
            mBtnHit.setVisibility(View.GONE);

            mTvHit.setVisibility(View.GONE);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    flipDealerSecondCard();

                }
            }, 7000);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    displayLost();

                }
            }, 7500);

        } else { //no blackjack

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    mBtnStand.setVisibility(View.VISIBLE);
                    mBtnHit.setVisibility(View.VISIBLE);
                    mTvHit.setVisibility(View.VISIBLE);
                    mTvStand.setVisibility(View.VISIBLE);

                }
            }, 6500);

        }

    }

退出按钮:

public void btnQuit_clicked(View view) {//game session end, go to main hall

        //TODO: clear the values in pref file??
        //clear whatever that was saved (data and key) in the shared preferences's file
        SharedPreferences.Editor spEditor = mPreference.edit();
        spEditor.clear();
        spEditor.apply();

        this.finish();

    }

再次播放按钮:

public void btnPlayAgain_clicked(View view) { //game session not yet end

        //restart game

        //TODO: clear the values in pref file??
        //clear whatever that was saved (data and key) in the shared preferences's file
        SharedPreferences.Editor spEditor = mPreference.edit();
        spEditor.clear();
        spEditor.apply();

        this.recreate();

    }

onpause()方法:

@Override
    protected void onPause() {
        super.onPause();

        SharedPreferences.Editor spEditor = mPreference.edit();

        spEditor.putInt(PLAYER_TOTAL_KEY, pTotal);
        spEditor.putInt(PLAYER_TOTAL_ALT_KEY, pTotalAlt);

        spEditor.putInt(DEALER_TOTAL_KEY, dTotal);
        spEditor.putInt(DEALER_TOTAL_ALT_KEY, dTotalAlt);

        spEditor.putInt(DEALER_NUM_HIT_KEY, mDNumHit);
        spEditor.putInt(PLAYER_NUM_HIT_KEY, mPNumHit);

        spEditor.putInt(DEALER_CARD_1_KEY, dCard1);
        spEditor.putInt(DEALER_CARD_2_KEY, dCard2);
        spEditor.putInt(DEALER_CARD_3_KEY, dCard3);
        spEditor.putInt(DEALER_CARD_4_KEY, dCard4);
        spEditor.putInt(DEALER_CARD_5_KEY, dCard5);

        spEditor.putInt(PLAYER_CARD_1_KEY, pCard1);
        spEditor.putInt(PLAYER_CARD_2_KEY, pCard2);
        spEditor.putInt(PLAYER_CARD_3_KEY, pCard3);
        spEditor.putInt(PLAYER_CARD_4_KEY, pCard4);
        spEditor.putInt(PLAYER_CARD_5_KEY, pCard5);

        spEditor.apply();

    }

restoreoldcards()方法:

public void restoreOldCards(ImageView v, int i) {

        switch (i) {
            case 1:
                v.setImageResource(R.drawable.clubs_a);
                break;
            case 2:
                v.setImageResource(R.drawable.clubs2);
                break;
            case 3:
                v.setImageResource(R.drawable.clubs3);
                break;
            case 4:
                v.setImageResource(R.drawable.clubs4);
                break;

....other cases...

        }

    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题