Oncreateoptionsmenu called multiple times

Oncreateoptionsmenu called multiple times

Author: Ask On: 06.06.2017

Here is my problem. I have an app where I am using ActionBar Sherlock with tabs, fragments with option menus. When I run the app I can see that the onCreateOptionsMenu is being called multiple times, for all the different fragments. I tried posting the most code as possible without being overwhelming, if you find that something is missing, please advise.

One thing that I notice is that everything is being called multiple times except for the onCreate method which is being called only once.

Ok, I started tracing back into Android code and found this part here that I edited to shorten this post.

eclipse - Android Min SDK Version vs. Target SDK Version - Stack Overflow

The problem is that mAdded does indeed have multiple instances of FragmentList1 in it, so the onCreateOptionsMenu method is "correctly" being called 3 times, but for different instances of the the FragmentList1 class.

What I don't understand is why that class is being added multiple times But that is a hell of a good lead. I seem to have found the problem s.

Exchange Rate. Money Management | pupuzifecose.web.fc2.com

I say problem s because on top of the multitude of menus, there is now also an Exception. My TabListener would then add a FragmentList1 to the FragmentManager. When the new Activity is created after rotation it would do two things:. Basically if the fragment is already in the FragmentManager there is no need to add it, just show it. But there lies the problem. It's not the same Fragment! It's the Fragment that was created by the earlier instance of the Activity.

So it would try to attach and show this newly created Fragment which would cause an Exception. So upon Activity creation I have to check to see if the Fragments are already in the FragmentManager.

If they are I use those instances, if not then I create new ones. This is done for all three tabs. You may have noticed that there are two similar labels: Ah, this is lovely In ordrer to use my TagListener polymorphically I have declared the following non static variable in the base class:.

It is assigned from inside the descendents and allows me to look in the FragmentManager for the different descendents of FragmentListBase. But I also need to search for specific descendents BEFORE they are created because I need to know if I must create them or not , so I also have to declare the following static variable.

Another simple change you might want to make is calling transaction. I had this very similar issues with "stackable" menus on rotation. I don't use tabs but I do use ViewPager with FragmentStatePagerAdapter so I can't really reuse my Fragments. After banging my head for 2 days I found very simple solution.

Indeed the problem seems to be with onCreateOptionsMenu called multiple times. This little code snippet takes care masks? What worked for me was moving the setHasMenuOptions true to the calling activity ie the activity in which the fragment was declared. I previously had it in the onCreate method of the fragment.

You still can add and remove fragments as shown in the Adding Tabs section of http: 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: This is the setting: MikeWallaceDev 2 7 My TabListener would then add a FragmentList1 to the FragmentManager 2 rotating the device would destroy the Activity as expected, but would not destroy the Fragments. When the new Activity is created after rotation it would do two things: This is what was causing the multitude of Menus call onTabSelected via setNavigationMode which would perform the following code: So it would try to attach and show this newly created Fragment which would cause an Exception The Solution.

There were a few things to do in order to fix all of this. Simply moving the call to bar. Thanks for the answer Jake, however: I added that to the code but no change. I did add more info to my question though Just a quite note on your polymorphic tag frustrations. Declare your base class like so: Dave 3, 3 22 It would depend on your needs I guess.

Here is the code snippet: At least on Honeycomb related SDK's the problem is solved by adding android: 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. This answer is overly complicated. 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.

inserted by FC2 system