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); } }