Saving data in edittext when hitting Back button

限于喜欢 提交于 2019-12-03 08:47:56

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();        
}
}

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();
    }
}

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!

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!