2

For some reason, every time the screen orientation is changed my app closes the context menu if it was open. All applications I have seen keep menus open after rotating the screen, but I cannot figure out why my app is closing them.

I am not handling configuration changes on my own and my onCreate method, which I know is called after every orientation change, does not touch menus at all. I would appreciate any insights on this problem.

FernandoPR
  • 123
  • 2
  • 6
  • I'm not adding this as an answer, because I'm an unsure if I am correct... but I believe you will have to handle this state and store it in an overridden onSaveInstanceState() call. – Maximus Apr 18 '11 at 16:58
  • I solved it! I was just forgetting to call super.onSaveInstanceState() from within my own onSaveInstanceState() method, so the default state information that is usually saved was not being saved. – FernandoPR Apr 18 '11 at 19:41

1 Answers1

1

The contextMenu disappeared because by default when rotating android calls destroy() and then onCreate() but :

If you don't want Android to go through the normal activity destroy-and-recreate process; instead, you want to handle recreating the views yourself, you can use the android:configChanges attributes on the element in AndroidManifest.xml.

<activity
    android:name=".MainActivity"
    android:launchMode="singleTask"
    android:configChanges="orientation|keyboardHidden">
</activity>

This way contextMenu is not closed when phone rotates, because onCreate() method is not called.

This topic can also be helpful - Activity restart on rotation Android

Community
  • 1
  • 1
evilone
  • 22,410
  • 7
  • 80
  • 107
  • I understand the destroy-and-recreate process. What I don't understand is how I am interrupting the default behavior. I created a simple activity with only the following code: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.layout.menu, menu); return true; } When I run this and flip the orientation, the menu remains open. – FernandoPR Apr 18 '11 at 18:52