0

I've handled screen rotation config changes in the android manifest, which works for dialog themed activities, however for these menu groups, which open after selecting a menu item (in onOptionsItemSelected) still close when I rotate the screen. Can I handle these in onConfigurationChanged? Or is there a better way? I've attached the code that opens the submenu.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   if (item.getGroupId() == R.id.submenu) {
      if (item.getItemId() == this.submenu) {
         return true;
      }
      this.value = item.getItemId();
      item.setChecked(true);
      //do something with value
      return true;
   }
   //...
   return super.onOptionsItemSelected(item);
}
Phil
  • 35,852
  • 23
  • 123
  • 164

2 Answers2

3

you will need to override

    @Override
 public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
 }

But to do this you must specify the configuration change you will handle yourself by adding to the manifest file on the Activity level the tag

     android:configChanges=["mcc", "mnc", "locale",
                             "touchscreen", "keyboard", "keyboardHidden",
                             "navigation", "orientation", "screenLayout",
                             "fontScale", "uiMode"]

on onConfigurationChanged save the state and reload it on onResume

Hazem Farahat
  • 3,790
  • 2
  • 26
  • 42
  • Hazem, Thanks for your response. I have this variable in my manifest file (and I override onConfigurationChanged), and it handles orientation and keyboard changes successfully: `android:configChanges="orientation|keyboard"`. Unfortunately, this still causes the group to close. Is there an activity configChanges variable that will keep this menu item open? – Phil May 20 '11 at 19:19
0

This ended up being a very simple fix. I needed to add the line android:configChanges="orientation" to my activity in the AndroidManifest.

Phil
  • 35,852
  • 23
  • 123
  • 164