Question

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

Was it helpful?

Solution

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.

OTHER TIPS

This probably is happening becoz the ondestroy or onpause method is called in the idle time. The ondestroy would be called in cased the memory is usage of app is high. The only solution in my opinion is to save data in in a local DB as it is being entered, and re populate it onresume.

To solve your problem in onPause() method of your Activity take the values inserted by the user in the static variables (you will require the number of variables how much EditText you have) and store the values in variables and in onResume() put the values back on to EditText.

package com.zeus.program;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

    public static String username;
    public static String password;


    EditText usernameET;
    EditText passwordET;


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

        usernameET = findViewById(R.id.username);
        passwordET = findViewById(R.id.password);
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        usernameET.setText(username);
        passwordET.setText(password);

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        username = usernameET.getText().toString();
        password = passwordET.getText().toString();

    }   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top