After changing the orientation of the options, the menu of the fragment does not disappear

I implemented my layout based on this tutorial: http://android-developers.blogspot.hu/2011/02/android-30-fragments-api.html

The differences are as follows:

  • I have different snippets to show based on the selection in the left list
  • "Fragment details" (those that go to the right) have different parameter menus

My problem is that if I already selected something on the left and then turned the phone to the portrait, the last menu options still exist and are visible.

I think the problem is with the last active fragment of the β€œpart” that is recreated after a change in orientation. for verification, I created these two methods:

@Override public void onStart() { super.onStart(); setHasOptionsMenu(true); } @Override public void onStop() { super.onStop(); setHasOptionsMenu(false); } 

And I show the right fragment as follows:

 case R.id.prefs_medicines: if (mDualPane) { // Check what fragment is shown, replace if needed. View prefsFrame = getActivity().findViewById(R.id.preferences); if (prefsFrame != null) { // Make new fragment to show this selection. MedicineListF prefF = new MedicineListF(); // Execute a transaction, replacing any existing // fragment with this one inside the frame. FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.preferences, prefF); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } } else { // Otherwise we need to launch a new activity to display // the dialog fragment with selected text. Intent intent = new Intent(); intent.setClass(getActivity(), MedicinePrefsActivity.class); startActivity(intent); } break; 

in one of my "details" snippets. when I debug it, it is called after rotation.

The problem is in the pictures:

1: in the landscape it is normal Landscape mode http://img834.imageshack.us/img834/8918/error1d.png

2: in portrait: optionsmenu not required Portrait mode http://img860.imageshack.us/img860/8636/error2r.png

How can I get rid of optionsmenu in portrait mode?

+4
source share
1 answer

I had the same problem and decided to install it by setting setHasOptionsMenu (true) in the fragment only if savedInstanceState is NULL. If onCreate receives the package, then the fragment is restored with a change in orientation to the portrait, so do not display the menu.

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(savedInstanceState == null) { setHasOptionsMenu(true); } } 
+5
source

All Articles