Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity and Fragment Options menu ordering [closed]

I have at most two option menu items created by my activity. Depending on the current fragment being displayed (changes using a tab control), I display another one or two items.

All the items created should always show (3 at most at any given time).

My question really is about the ordering of the items. I can't find any standard on this. Should my activity's items be far right and then fragment items to the left of those? or vice versa?

Thanks

like image 527
Johan Avatar asked Mar 19 '23 14:03

Johan


1 Answers

They can be any order you like if you use the android:orderInCategory attribute in your menu XML.

If you don't specify this value, then it will default to something (probably zero). Since all your items will have this default, then the order will depend on the following:

  • What order you declare the items in XML
  • What order your fragments get the onCreateOptionsMenu callback.

The Activity gets the onCreateOptionsMenu callback first, so those items will be added to the menu object first. Then it dispatches the same call to its fragments. I haven't gone deep enough into the source code to tell you how Android decides which fragment gets the callback first (if there even is a defined order), but since orderInCategory is the same, the items should be added to the end of the menu. So you would end up with

  • Activity's menu items in XML order
  • "First" fragment's menu items in XML order
  • Next fragment's menu items in XML order
  • etc.
like image 55
Karakuri Avatar answered Apr 01 '23 05:04

Karakuri