Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText sometimes loses input when App resumes

I've made a rudimentary Data logging app, which is supposed to have the user input values into a dynamic and often large number of EditText views. Once the user has finished entering all of the Data, a button can be pressed which takes a picture of the data entry (bear with me about storing the input as a picture, there are specific reasons for this), and saves the picture to storage.

Problem: It works very well, except for one particular issue. On occasion, when the user puts the phone into idle (which occurs frequently before the input actually gets saved), all of the input in the EditText views will clear (making me a very angry/sad person). What's more, it's only ever occured when I'm in the field and unable to look at the log files in eclipse to figure out what methods might be being called differently than normal.

Question: Why is it that the values in my EditTexts persist most of the time when the phone goes to and from idle, yet they clear on seemingly random (and often inconvenient) occasions?

I have a feeling that this is caused by onStop() being called instead of onPause() (or perhaps vice versa) on certain occasions, but my knowledge of these methods isn't quite up to speed yet. Also, I'm aware that this issue could be solved via a sharedPreferences file (or some such idea), but I would like to know what is occuring for the sake of understanding more than anything else.

Details:

-I'm not sure if any of this code might be relevant to the question in a meaningful way, but I'll include a snippet of my onCreate() method if it helps. Let me know if I should add anything else:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input_param);

    Intent intent = getIntent();
//Rest of the code just builds the layout from data taken from the intent

-I haven't overridden any of the lifecycle methods (i.e. onStop(), onResume(), etc.) except for onCreate(). None of the other methods in my activity should have anything to do with the issue/ as far as I can tell

like image 806
Ryan Avatar asked Nov 11 '22 04:11

Ryan


1 Answers

I believe that the issue is that at times when your app is going "idle" (i.e. going to the background, screen off) that Android is automatically cleaning it up and destroying all data, including that in your EditTexts.

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

http://developer.android.com/reference/android/app/Activity.html

To fix this issue you can override the onSavedInstanceState() method to save your EditText values and then onCreate() will pull those values back when you go back to the activity. You will then need a check in place to see if you need to repopulate your EditTexts from those values or if they were null. More on this here.

like image 76
TronicZomB Avatar answered Nov 15 '22 06:11

TronicZomB