1

When the device rotates, I have different layouts based on portrait/landscape. So I can't put this line in manifest:

android:configChanges="orientation|screenSize|keyboardHidden"

However, if the user is in a EditText field and the keyboard is out, and text is filled in that field, and then they rotate device, keyboard hides and text is lost (frustrating to user).

Is there a proper way to handle this situation?

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

1 Answers1

1

Why couldn't you use android:configChanges and then override the onConfigurationChanged and then store whatever is in the EditText in a temp variable, call you UI initialization method (take out the findViewById from onCreate and put it in a seperate method), and then set the EditText to the temp variable.

Here is a sample of something I did for buttons

@Override

public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
    Log.i("configChange", "configChange");      
    String tmp = connect.getText().toString();
    boolean onTmp = on.isEnabled();
    boolean offTmp = off.isEnabled();
    boolean connTmp = reconnect.isEnabled();
    InitializeUI();
    connect.setText(tmp);
    on.setEnabled(onTmp);
    off.setEnabled(offTmp);
    reconnect.setEnabled(connTmp);
}
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • I am using Fragments. Would I put this in the Parent Activity, or the Fragment with the EditText? Good answer by the way, I see what you are doing... – TheLettuceMaster Feb 21 '13 at 19:30
  • I am actually relatively new to Android and have not worked with Fragments yet. I am working with the Android Open Accessory Protocol and this was the way I was able to get around the rotation calling onDestroy and onCreate, which would mess up the USB accessory enumeration. My guess would be which ever is going to be redrawn, if the entire parent activity is redrawn then in there, if only the fragment then put it in there I think. If you figure out which one or if someone else knows then I would like to know so that I can learn which is better too. – TronicZomB Feb 21 '13 at 19:36
  • @KickingLettuce Out of curiosity, did you ever get this to work for your problem? – TronicZomB Mar 06 '13 at 14:29
  • I am sorry, I had to change course and decided to continue using: `android:configChanges="orientation|screenSize|keyboardHidden"` but I'll mark yours correct because the research I found said it was on the right track.... – TheLettuceMaster Mar 06 '13 at 16:56