简体   繁体   中英

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM