SharedPreference - 如何在 Activity 收到数据时仅吐司一次

SharedPreference - How to toast only once when Activity recieves the data

当我的 Activity 2 从我的 Activity 1 接收数据/值时,我怎么能只烤一次? ,当他从另一个 activity 收到相同的 value 时,我正在尝试解锁 button。 下面是我的代码

SharedPreferences sharedPreferences = getSharedPreferences("UnlockPreTestFour", Context.MODE_PRIVATE);
             final int scorePre = sharedPreferences.getInt("ScoreFour", 0);
               if(scorePre !=0){
            Toast.makeText(grade_four.this, "Button Unlock!", Toast.LENGTH_SHORT).show();
                      }

在我的代码中 Activity 2Activity 1 接收到相同的值后, 它总是 toast 因为它已经有一个 value ,有人可以帮助我吗,谢谢

Boolean hasReceivedData = false

SharedPreferences sharedPreferences = getSharedPreferences("UnlockPreTestFour", Context.MODE_PRIVATE);
         final int scorePre = sharedPreferences.getInt("ScoreFour", 0);

           //check to see if you have received the data
           if(scorePre !=0 && hasReceivedDate == false){
                Toast.makeText(grade_four.this, "Button Unlock!", Toast.LENGTH_SHORT).show();

                //set variable to ensure data is only loaded once
                hasReceivedData = true;
             }

这是一个简单而有点老套的答案。您的 hasReceivedData 变量必须在 ConfigurationChanged 上传递。希望对您有所帮助!