Guys i have created an app that updates the text view on button click. It gets the text from a array that i have created. But whenever i close the app and relaunch the app it takes me back to the default text(in my case blank text). Can anyone tell me how to fix this problem. The apk for the App can be downloaded here. Below i have added the Manifest and Java Files of my project here. Thanks you very much
-
Can you provide some samples of you code that indicate where you are having trouble? Also, explain what you have already tried to resolve this yourself and why that didn't work. What is the expected behavior? – Karl Wilbur Sep 12 '15 at 05:12
2 Answers
You should read up about the Android Activity lifecycle, namely onCreate, onStart, onResume, onPause, onStop and onDestroy.
The main thing you should figure out is when to call the storing and retrieving data method on your choosing such as saving data to and retrieving data from State Bundle or SharedPreferences or even an SQLite Database within the Android Activity lifecycle.
This question and answers here will hopefully solve your problem, but some answers are not the most safe, for example the suggestion of using savedInstanceState.putString() within onSaveInstanceState() and using savedInstanceState.getString() within onRestoreInstanceState() like the accepted answer in the link provided might help depending on what you want to do with your app, however there are some other suggestions in the answer thread as well such as saving to State Bundle and SharedPreferences or even use a SQLite Database as suggested in the thread.
-
Instance state is not saved or restored when an activity is finished or killed, only when it's destroyed in the background or backstack. – Kevin Krumwiede Sep 12 '15 at 05:27
Saving the activity state will not help if you want the changes to persist when the activity is finished or killed. You'll need to use some other persistence mechanism. Since it's a simple piece of data, the best solution is probably to save the button text (or its resource ID) in a SharedPreferences
file.

- 9,868
- 4
- 34
- 82