1

My understanding is that when you change the device orientation, your app gets destroyed and created again. This means that onCreate() should run every time I rotate the device.

I'm trying to set an EditText every time the app is created, including whenever the screen is rotated. So, I start up the app, and the text field says "Bap". I then change the contents of the field to "Boop", then rotate the screen. I would expect the field to now say "Bap", but it doesn't, it remains whatever it was after I changed it, before the orientation change. Why is this?

My onCreate() looks like this:

@Override
public void onCreate(Bundle savedInstanceState)
{    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText editText = (EditText)findViewById(R.id.edit_message);
    editText.setText("Bap", TextView.BufferType.EDITABLE);
}

It's worth noting that if I move the setText() call into onResume(), everything works as I would expect it to, with the text field resetting every time I rotate the device.

NOTE: I realize a similar question has been asked, but the answer given doesn't really explain why this behaviour is occuring.

Community
  • 1
  • 1
Lewis
  • 1,310
  • 1
  • 15
  • 28
  • you need check when orientation changed, the `oncreate` method has be invoked. – idiottiger May 08 '12 at 03:47
  • Sound like you explicitly define android:configChanges in AndroidManifest.xml. Check out [Activity API - Configuration Changes section](http://developer.android.com/reference/android/app/Activity.html) to see how the default behaviour could be altered. – yorkw May 08 '12 at 03:54
  • @yorkw: Just checked now, and AndroidManifest.xml makes no mention of android:configChanges. – Lewis May 08 '12 at 04:00

3 Answers3

3

It is happening because the Android framework is doing what it is supposed to. It is saving a bundle (SavedInstanceState) with all the information on the current state of your app, to be used when it re-creates your view after the orientation change.

Did you leave some code out of the onCreate you posted? Maybe something like:

if (savedInstanceState == null)

If your onCreate code is wrapped in an if like that, that is why you are not seeing the results you expect.

It is standard practice to have an onCreate that looks like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
                // Your first time start-up code here, will not run on orientation change   
    }
            // Any code you want to run EVERY time onCreate runs goes here
}

This is done so you don't needlessly re-create everything on an orientation change.

Hope this helps!

EDIT

Looking into it further, it looks like what is happening is this (gleaned from here):

device orientation change

onSaveInstanceState(Bundle outState)
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onRestoreInstanceState(Bundle inState)
onResume()

It's basically saying that the bundle is getting passed to onRestoreInstanceState, which occurs after onCreate, which would explain what is happening in your case.

So, the solution would be to move the setting of the EditText value to your onResume method (or overriding onRestoreInstanceState ).

Barak
  • 16,318
  • 9
  • 52
  • 84
  • Hm, okay. I don't have anything wrapped in an if statement as you mentioned. If Android is doing any of this recreation after onCreate()'s completion, then what I'm seeing would make sense. Do you know if that's the case? – Lewis May 08 '12 at 04:07
  • Hmm, if you don't have that if statement in there, then I'm with you. I believe it should be re-setting that EditText. I'll look aroudn some more and see if I can come up with a plausible explanation. Just some background info.. is this app using fragments? – Barak May 08 '12 at 04:15
1

Yes. When the activity recreated, the edittext can not be set any value in onCreate. you should changed in onResume().

Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
1

I decided to dig into the savedInstanceState that is passed into the onCreate method, and I found this:

Key: android:viewHierarchyState
Value: Bundle

That bundle had this key, value pair:

Key: android:views
Value: SparseArray

That SparseArray had this value: TextView.SavedState{41857898 start=4 end=4 text=Boop}

So what's happening is that Android is automatically restoring the state of some Views, presumably in the onRestoreInstanceState method.

To prevent this behavior, in XML, add this line to your EditText:

android:saveEnabled="false"

To do this programmatically, call view.setSaveEnabled(false).

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • 1
    This answer just saved my bacon. Who down-voted it? Crazy. I absolutely could not figure out what was going on with the EditTexts when my activity was rotated. Also, the EditTexts in my activity were part of a custom "compound" view (an EditText and a FilterSpinner in a RelativeLayout), I couldn't simply do the updates to the EditTexts in the activity's onResume (the activity wasn't even supposed to be aware of the EditTexts inside the "compound" views!). I just set the EditText's saveEnabled key to false in the "compound" view's XML layout and everything is now working as expected. Thanks! – spotcatbug Dec 19 '12 at 16:34