1

I'm following this to create a navigation drawer. Inside one of my fragments, I have an EditText. I want that the text on the EditText doesn't change if I rotate the screen.

I've tried to use Bundle savedInstanceState but it doesn't work. I reed that I should remove the line android:configChanges from my Manifest, but I don't have that line.

I've tried with the correct answer of this question, but it's still not working for me. So what should I do? Thanks :)

Community
  • 1
  • 1
andrew
  • 3,879
  • 4
  • 25
  • 43

1 Answers1

0

If you don't have the android:configChanges line, then Android will handle the screen rotation. Your app will get destroyed and recreated. Before the app is destroyed, Android calls onSavedInstanceState where you have a chance to save data for the recreation. When the app is recreated, the bundle passed to onCreate is not null and contains your saved state.

Try

public void onSavedInstanceState(Bundle bundle) {
    bundle.put("editTextString",et.getText().toString());
    super.onSavedInstanceState(bundle);
}

and in

public void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState!=null) {
         String savedText = savedInstanceState.get("editTextString");
         et.setText(savedText);
    }
} 
ElDuderino
  • 3,253
  • 2
  • 21
  • 38
  • if I try as you say, I get a "The method onSavedInstanceState(Bundle) is undefined for the type Fragment" error. If I change to super.onSaveInstanceState(bundle); it still doesn't work, and I've already tried it before :( – andrew Dec 19 '13 at 16:44