-1

I have two screens in my app, screen "a" and "b". Screen "b" has a button, that is defined by layout file. I need to change the text of the button from code. I managed to do this by using button.setText(), but when i change screen to "a" and back to "b" the text is being changed back to initial( to text which is specified in layout fail). I need to avoid such behavior, which means i need the changed text after changing of screen. Thanks!

Unknwn Artist
  • 430
  • 5
  • 17

1 Answers1

0

You could use shared preferences to save the button text:

To set the text after starting activity (onCreate):

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String buttonText = sharedPref.getString("buttonText");
if (buttonText != null) {
   button.setText(buttonText);
}

To save the new text:

button.setText(newButtonText);
sharedPref.editor.putString("buttonText", newButtonText).commit();
Prexx
  • 2,959
  • 5
  • 31
  • 48