Oncreateoptionsmenu called multiple times

Oncreateoptionsmenu called multiple times

By: PLAGi Date of post: 12.06.2017

My app has two fragments , one a listing and one a detail view, which are both shown in landscape mode and only the listing is shown in portait. On configuration change, if I put a breakpoint in onCreateOptionsMenu of both fragments , they are being hit twice and the menu items in the Actionbar are being duplicated.

I tried setting invalidateOptionsMenu in different events, like onResume and onActivityCreated , but that doesn't seem to help. I'm not sure what would cause onCreateOptionsMenu to be hit twice. I noticed there was a solution here , but that doesn't work for me. It causes a java. IllegalArgumentException No view found for for fragment in onStart on the Activity.

This is the main. If you add a fragment on the layout, the android framework will instantiate it for you. In the code you posted, you are having 4 Details fragment and 2 list fragments instances total, even though you might not be able to see them all.

Instead of instantiating the fragments manually, you should retrieve it's instance by calling FragmentManager. For the details fragments, instead of having one fragment declaration on the layout, i would replace it with a normal layout that you can use to add your pager adapter. By posting your answer, you agree to the privacy policy and terms of service.

Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us. Log In Sign Up. Join the Stack Overflow Community.

Stack Overflow is a community of 7. Join them; it only takes a minute: UPDATE I am using a ViewPager to load the fragments when in portait mode: Kris B 1, 8 47 Are you adding different actions in each of the fragments? I would double check the part where you add your fragment in you layout.

Make sure your fragments are not added twice.

android optionsmenu - onCreateOptionsMenu() called twice in Fragment - Stack Overflow

RobertEstivill rciovati Sorry, I added the code where I'm adding the fragments. In landscape mode I'm adding them via XML and in portrait I'm adding them via a ViewPager.

android - onCreateOptionsMenu called twice on configuration change - Stack Overflow

I know with a ViewPager the next fragment is loaded, for performance, could that be causing the issue? Each fragment is called twice, once for the listing and once for the details, even in landcape.

Robert Estivill 7, 5 24 I'm not sure what you mean by "adding them on the layout file and also instantiating them manually"?

The layout file way is only used in landscape mode it's in a layout-land folder , while the instatiating them manually happen in portait mode only.

Wouldn't that mean they are handled separately? I'm new to using fragments and viewpagers so I may just be misunderstanding something basic. The activity is instantiated no matter what orientation the device is in.

There's nothing on your code that points to the fact that the activity only runs on portrait. If you add anything to a layout file, the framework will instantiate it for you, you don't have to call it's constructor, just get the reference to the already created instance.

Is like adding a TextView to the layout. You don't instantiate the TextView again on your activity, you just get the reference with findViewById to start working with it.

The same thing happens with fragments. I see where the confusion is. I left out code where I'm checking if the viewpager is null in the activity. So if the device is in portrait mode it's using the layout that contains the viewpager. If it's in landscape, then it's using the layout that doesn't contain the viewpager , so there shouldn't be the double-instantiating going on.

I will update the OP. Sign up or log in StackExchange. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with JavaScript enabled. Please post de code of your fragments.

java - Why does onCreateOptionsMenu run multiple times? - Stack Overflow

For the details fragments, instead of having one fragment declaration on the layout, i would replace it with a normal layout that you can use to add your pager adapter share improve this answer. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

Rating 4,9 stars - 796 reviews
inserted by FC2 system