Seraphis
Seraphis

Reputation: 1036

Android EditText content multiplies when orientation changed

I have weird situation with EditText. Situation:

  1. set device to landscape mode
  2. enter text to EditText and press "DONE"
  3. change orientation
  4. EditText contains doubled text that was typed

the code:

private EditText enterText;

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

    enterText = (EditText) findViewById(R.id.enterText);
}

If I comment the line with findViewById, everything is working properly.

Any ideas why this happens?

EDIT: I've double checked and it happens every time, not only if the line I was written about is commented.. :-\

EDIT2: It happens only on first orientation change event. Every next is OK..

Upvotes: 0

Views: 710

Answers (1)

Mazze
Mazze

Reputation: 1404

Every time you rotate your phone the Activity restarts, which probably calls something that causes your text to be written again somehow. This is part of the OS and a bit annoying, here is that to do.

If you in your manifest add:

android:configChanges="orientation"

to the activity node in which the rotation is done, you'll have to override the onConfigurationChanged.

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);

}

Whenever your phone rotates the overriden method will call and you can do a special initialization like ignore the input if there was an input earlier and such. Hope this gets you on the right track!

PS: You can also pipeline the configChanges with different other "values/configs". Such as keyboardHidden and many others.

They are listed on the following link

Upvotes: 1

Related Questions