I have an activity (act2), which can be launched from act1 or act3. If it is launched from act1, I would like for my option menu to be available, meaning when you press the menu button, it shows up. If it is launched from act3, I have nothing that needs to be in the option menu so I would like for it not to appear when you press menu. How would I go about this?
Share
If you are invoking act2 from act1 pass a boolean variable as
truethrough bundle. And if you are invoking act2 from act3 pass it asfalseor don’t pass it at all.In your act2 and specially in
onCreate()method catch that boolean variable. You have to be careful that if you are not passing a bundle from act3 it will thrownull pointer exceptionin act2 while doing this operation. Now you can get a boolean variable that helps act2 to determine where it came from. [if its true than it came from act1 or from act2].now override
onCreateOptionsMenu()method in your activity, it returns boolean. So if you passtruethan the menu items will pop up when you press menu button, or no menu items will pop up if you returnfalse. Use that boolean variable you fetched from onCreate() method, you can simply return it so when itstrue[from act1] menu will be shown or in case offalse[from act3] it will not be shown at all.