Saving data in edittext when hitting Back button

12,046

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

Solution 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();
    }
}
Share:
12,046

Related videos on Youtube

Mike
Author by

Mike

Updated on August 02, 2022

Comments

  • Mike
    Mike almost 2 years

    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.

  • Mike
    Mike over 10 years
    I forgot to put this in the original question but when the app is exited by the home button the saved data needs to removed. From what I understand SharedPreferences is mostly for information that is needed for along time. Is it still ok to use SharedPreferences and then delete them when exiting the app?
  • Martin Cazares
    Martin Cazares over 10 years
    That's right SharedPreferences should be used to persist data only for long term even when the app is closed or phone is shutdown, so if you are talking about pressing home maybe this approach wouldn't suit your needs and you should try to approach this issue with some locally cached objects...