Recently I started working in Android Studio, constantly contacting me to solve questions on this resource, but now I haven’t found the answer.

The task is to rewrite the value "1" in SharedPreferences , available for testing and reading on all activities. The initial value is "0", the value changes after pressing the button (which then becomes unavailable).

Problem - After pressing the button, it disappears, however, when you return to the same activity, it becomes available again. Probably I incorrectly set conditions or not truly made SharedPreferences . The code is given below:

 public class Luis_Room_Activity_act1 extends AppCompatActivity { SharedPreferences sPrefLKC = getSharedPreferences("val", Context.MODE_PRIVATE); int val = sPrefLKC.getInt("val", 0); 

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

 public void onClickButton3(View view) { if (getSharedPreferences("val", Context.MODE_PRIVATE) == getSharedPreferences("val", 0)) { SharedPreferences sPrefLCK = getSharedPreferences("val", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sPrefLCK.edit(); editor.putInt("val", 1); editor.apply(); TextView txt = (TextView) findViewById(R.id.textViewButton3); txt.setText(R.string.textViewEmpty); view.setEnabled(false); } else { TextView txt = (TextView) findViewById(R.id.textViewButton3); txt.setText(R.string.textViewEmpty); view.setEnabled(false); } } 

    2 answers 2

    There are a lot of incomprehensible actions in your code that should be ignored. As a trigger, it is more rational to use a Boolean variable, which will relieve the checks in if :

     public class LuisRoomActivityAct1 extends AppCompatActivity { SharedPreferences sPrefLKC = getSharedPreferences("val", Context.MODE_PRIVATE); boolean isEnabled = sPrefLKC.getBoolean("val", true); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.luis_room_act1_layout); Button btn = (Button) findViewById(R.id.button); // кнопка, на которою нажимать для скрытия. btn.setEnabled(isEnabled); // восстанавливаем значение } public void onClickButton3(View view) { SharedPreferences.Editor editor = sPrefLCK.edit(); editor.putBoolean("val", false); editor.apply(); TextView txt = (TextView) findViewById(R.id.textViewButton3); txt.setText(R.string.textViewEmpty); view.setEnabled(false); } } 

    PS: In Java, it is customary to refer to classes with a capital letter in the CamalCase style, and not through dashes like yours.

    • Thank you very much! - Jacob Zaitsev
     public void onClickButton3(View view) { if (sPrefLKC.getInt("val", 0) == 0) { SharedPreferences.Editor editor = sPrefLKC.edit(); editor.putInt("val", 1); editor.apply(); view.setEnabled(false); } else { //... } } 

    When you return to the previous activation, the button is available again, because setEnabled(false) performed ONLY when you click on the button, respectively, you need to check the contents of the Preferences also in onCreate()

     @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.luis_room_act1_layout); if (sPrefLKC.getInt("val", 0) == 1) { Button button3 = (Button) findViewById(R.id.button3); button3.setEnabled(false); } } 
    • Thank you very much! - Jacob Zaitsev