Saving data in edittext when hitting Back button

2019-04-02 08:24发布

问题:

So on activity 1 I click a button that takes me to activity 2. In activity 2 I enter some data into an EditText. When I hit the back button on the phone it takes me to activity 1 which is correct but if I hit the activity 1 button again any text that I entered into the EditText is gone. I am sure this is because I am starting a new Intent every time I hit my button and I think I need to be using Flags but I am not certain. Below is my basic MainActivity1 and MainActivity2 without the code I tried that didn't work.

**Edit: After exiting the app all saved data needs to be deleted.

MainActivity1

public class MainActivity extends Activity {

Button b1;

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


    b1 = (Button)findViewById(R.id.button2);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent i = new Intent(MainActivity.this,MainActivity2.class);
            startActivity(i);


        }
    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

MainActivity2

public class MainActivity2 extends Activity {


EditText et1;

@Override
protected void onCreate(Bundle outState) {
    super.onCreate(outState);
    setContentView(R.layout.activity_main_2);

    et1 = (EditText)findViewById(R.id.editText1);

    }
}

}

Thank you in advance.

回答1:

Try this copy these codes in your MainActivity2 it will save your data (i.e. your EditText Data)

public class MainActivity2 extends Activity {


EditText et1;

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

et1 = (EditText)findViewById(R.id.editText1);
loadSavedPreferences(); 

}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this);

et1.setText(sharedPreferences.getString("string_et1",""));

}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
        .getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public void saveData(){
savePreferences("string_et1", et1.getText().toString());
}
@Override
public void onBackPressed(){
    saveData();
super.onBackPressed();
}   
}

if you want to delete the preferences when exiting the app try this in your MainActivity1.

public class MainActivity extends Activity {

Button b1;

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


b1 = (Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Intent i = new Intent(MainActivity.this,MainActivity2.class);
        startActivity(i);


    }
});
}
@Override
public void onBackPressed() {
    clear_pref();

}
public void clear_pref(){
            SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
            Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.commit();        
}
}


回答2:

You have to save your data in onBackPressed() somewhere. I suggest using Preferences:

public class MainActivity2 extends Activity {

    EditText et1;

    @Override
    protected void onCreate(Bundle outState) {
        super.onCreate(outState);
        setContentView(R.layout.activity_main_2);

        et1 = (EditText)findViewById(R.id.editText1);
        SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
        String restoredText = prefs.getString("text", null);
        if(!TextUtils.isEmpty(restoredText)){
             et1.setText(restoredText);
         }
    }
    @Override
    protected void onBackPressed(){
       SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
       editor.putString("text", et1.getText().toString()); 
       editor.commit();
       super.onBackPressed();
    }
}


回答3:

First of all you need to understand that by pressing back button on an activity means it is being explicitly destroyed, hence the saveInstanceState mechanism provided by the OS in case of change orientation is not an option, however you can always relay on the persistence mechanisms that android allows to store values, i recommend you to store the value on SharedPreferences and then populate it back to the edit text after re launching the activity...

Hope this helps.

Regards!



回答4:

You are correct, it is to do with using a new Intent.

However, it is possible to use Android's Bundle class to pass data between activities.

Perhaps this article will explain the concept and the method a little better.



回答5:

write code to save text in edittext in onDestroy function, you will have to override it. and when creating the activity again (in onCreate method), retrieve the data and populate in EditText. To save data use SharedPreference. see shared preference example and this