0

I'm saving the content of my EditText on rotation as

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("editTextText",mEdittext.getText().toString());
}

And in my onActivity created I'm trying to reset it to the previous value as

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mContext = getContext();
    View mView = getView();
    if (mView != null) {
        mEdittext = (EditText) mView.findViewById(R.id.editt_fragPend);
        if(savedInstanceState!=null){
            String savedText=savedInstanceState.getString("editTextText","");
            mEdittext.setText(savedText);
        }
    }
}

I debugged the above code the value of "savedText" indeed is the value I stored in onSaveInstanceState but after setting it to the editText. It still shows up blank. I have no other code that meddles with the editText.

San Dé
  • 43
  • 1
  • 6
  • the value should be restored automatically if you provide unique ids for edittext – Raghunandan Mar 10 '16 at 09:46
  • @Raghunandan If you mean give it a unique id in 'android:id='. Yes I did give it a unique ID. – San Dé Mar 10 '16 at 09:53
  • yes try it out yourself – Raghunandan Mar 10 '16 at 09:54
  • I think your fragment is regenerated on screen rotation and the value you are setting on previous fragment view. Just add a LOG on fragment onCreateView() to check you are updating on same fragment object or not. – Ashlesha Sharma Mar 10 '16 at 10:03
  • @AshleshaSharma ya it is getting regenerated. How do I solve this? – San Dé Mar 10 '16 at 10:38
  • You could follow two steps: 1. stop fragment regeneration in activity. or 2. stop recreation of activity and fragment on screen rotation. here you can set flags in android manifest. – Ashlesha Sharma Mar 10 '16 at 10:41
  • That was really vague but it helped thanks! I didn't know what to do to stop fragment regeneration. This helped http://stackoverflow.com/a/17135346/5618904 – San Dé Mar 10 '16 at 11:23

0 Answers0