0

I am very new to android and I have one doubt. I am working on one application and I have two activites in my appication. In first activity I have edittext and one button. Once I entered the text in edittext and click on button it will go to second activity. Now my doubt is, again if we come back from second activity to first activity the text which we have already entered in edittext view in first activity should be there i.e, we should able to see the text which we entered in edittext which we entered before going to second activity. How can I do it. Please help me.

Jayasree
  • 21
  • 4

2 Answers2

1

This is best done using SharedPreferences.
That will allow you to save values.
You will need to retrieve the stored data (if present) when the activity loads, and save it when you click the button.

//should be called after setContentView()
//requires class variable myEditText to be the EditText you want to change
public void RetrieveData(){
        SharedPreferences sp = getSharedPreferences("mySharedPreferences", MODE_PRIVATE);
        myEditText.setText(sp.getString("savedstring",""));
    }

//should be called in the OnClick event of the button
//requires class variable myEditText to be the EditText
public void SaveData(){
    SharedPreferences sp = getSharedPreferences("mySharedPreferences", MODE_PRIVATE);
    SharedPreferences.Editor spe = sp.edit();
    spe.putString("savedstring",myEditText.getText().toString());
}
x13
  • 2,179
  • 1
  • 11
  • 27
0

If you go back to first activity with the back button or this.finish();, yes the EditText will have the same text.

If you open up a new first activity with

Intent intent = new Intent(this, FirstActivity.class); startActivity(intent);

The EditText will be empty. In this case, you can pass your text in intent from 1st Activity to 2nd and then again from 2nd to 1st.

To send data in an intent, you can check this : How do I create an android Intent that carries data?

Else, use SQLite database or SharedPreferences (see other answer)

Community
  • 1
  • 1
xNeyte
  • 612
  • 4
  • 18