SharedPreferences spref; final String SAVED_TEXT = "saved_text"; public void savee(View view) { spref=getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed=spref.edit(); ed.putString(SAVED_TEXT, "hih"); ed.apply(); } public void loadd(View view) { TextView textView = findViewById(R.id.textView2); spref = getPreferences(MODE_PRIVATE); load=spref.getString(SAVED_TEXT,""); textView.setText(load); } 

Where is the mistake? It should save the word hih, but when loadd starts, nothing is output.

  • Is this code in one activation? If not, it will not work - YuriySPb
  • This code is not enough to solve your problem. Where are these methods located and when are they called from? - pavlofff

1 answer 1

It turns out the error is what I wrote getPreferences(); , but you need to write getSharedPreferences() .