0

I have two activities in my app now I want to save text of the editText, when user goes to next, which is given by the user and after coming back to MainActivity that value should be placed in editText my code is

public class MainActivity extends Activity {

EditText editText;
String name = null;

@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText1);
    if(savedInstanceState == null){
    editText.setText("Some Thing");
    }else{
        String newValue = savedInstanceState.getString("myData");
        editText.setText(newValue);
    }
    Log.d("ARSHAY....", "in onCreate()");
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onSaveInstanceState(savedInstanceState);
    editText = (EditText) findViewById(R.id.editText1);
    name = editText.getText().toString();
    savedInstanceState.putString("myData", name);

}


public void goToNextArsh(View view ) {


    Intent intent = new Intent(MainActivity.this, Second.class);
    startActivity(intent);
}
  }

but this does not set the new input on coming back to the MainActivity. does any body know how to do that?

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99
  • 1
    http://developer.android.com/training/basics/data-storage/index.html – donfede Feb 21 '13 at 04:20
  • Use [SharedPreference](http://developer.android.com/reference/android/content/SharedPreferences.html). Here is an [example](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Renjith Feb 21 '13 at 04:29
  • First! thanks to both of you. I have tried these all methods, Im getting value of editText like `getPreferences(MODE_PRIVATE).edit().putString("som.arshay.retreive.data", editText.getText().toString()).commit();` in onPasue() method and getting value back like `String newValue = getPreferences(MODE_PRIVATE).getString("som.arshay.retreive.data", editText.getText().toString()); editText.setText(newValue);` . but still Im unabled to achieve my desired result ... – Arshad Ali Feb 21 '13 at 04:55

2 Answers2

1

first of all why are you using editText = (EditText) findViewById(R.id.editText1); two times. use only in onCreate()

and use this code

@Override
protected void onPause() {
    SharedPreferences pref = getSharedPreferences("YOUR_KEY", MODE_PRIVATE);
    Editor edit = pref.edit();
    edit.putString("som.arshay.retreive.data", editText.getText().toString());
    edit.commit();
    super.onPause();
}

@Override
protected void onResume() {
    SharedPreferences pref = getSharedPreferences("YOUR_KEY", MODE_PRIVATE);
    String newValue = pref.getString("som.arshay.retreive.data", "");
    editText.setText(newValue);
    super.onResume();
}
Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
  • ok! this works, but there is still one bug in this code, that is Im un-able to see value in `editText` which I have set in `onCreate()` on first launch. Please suggest some solution. – Arshad Ali Feb 21 '13 at 11:09
0

Use these methods onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle savedInstanceState)

Prasanna
  • 236
  • 1
  • 6