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!
Asked
Active
Viewed 69 times
-1
-
post your code of both activitys – Ashish Shukla Oct 01 '15 at 11:31
-
where you place setText in oncreate() or other?? – Junaid Ahmed Oct 01 '15 at 11:32
-
See: http://stackoverflow.com/questions/151777/saving-activity-state-on-android?rq=1 – Gunnar Karlsson Oct 01 '15 at 11:35
-
in onClick method of other button – Unknwn Artist Oct 01 '15 at 11:36
1 Answers
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