FileDocCategorySizeDatePackage
SupportMenuInflater.javaAPI DocAndroid 5.1 API19978Thu Mar 12 22:22:56 GMT 2015android.support.v7.internal.view

SupportMenuInflater

public class SupportMenuInflater extends android.view.MenuInflater
This class is used to instantiate menu XML files into Menu objects.

For performance reasons, menu inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use SupportMenuInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R. something file.)

hide

Fields Summary
private static final String
LOG_TAG
private static final String
XML_MENU
Menu tag name in XML.
private static final String
XML_GROUP
Group tag name in XML.
private static final String
XML_ITEM
Item tag name in XML.
private static final int
NO_ID
private static final Class[]
ACTION_VIEW_CONSTRUCTOR_SIGNATURE
private static final Class[]
ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE
private final Object[]
mActionViewConstructorArguments
private final Object[]
mActionProviderConstructorArguments
private android.content.Context
mContext
private Object
mRealOwner
Constructors Summary
public SupportMenuInflater(android.content.Context context)
Constructs a menu inflater.

see
Activity#getMenuInflater()


               
       
        super(context);
        mContext = context;
        mActionViewConstructorArguments = new Object[] {context};
        mActionProviderConstructorArguments = mActionViewConstructorArguments;
    
Methods Summary
private java.lang.ObjectfindRealOwner(java.lang.Object owner)

        if (owner instanceof Activity) {
            return owner;
        }
        if (owner instanceof ContextWrapper) {
            return findRealOwner(((ContextWrapper) owner).getBaseContext());
        }
        return owner;
    
private java.lang.ObjectgetRealOwner()

        if (mRealOwner == null) {
            mRealOwner = findRealOwner(mContext);
        }
        return mRealOwner;
    
public voidinflate(int menuRes, android.view.Menu menu)
Inflate a menu hierarchy from the specified XML resource. Throws {@link InflateException} if there is an error.

param
menuRes Resource ID for an XML layout resource to load (e.g., R.menu.main_activity)
param
menu The Menu to inflate into. The items and submenus will be added to this Menu.

        // If we're not dealing with a SupportMenu instance, let super handle
        if (!(menu instanceof SupportMenu)) {
            super.inflate(menuRes, menu);
            return;
        }

        XmlResourceParser parser = null;
        try {
            parser = mContext.getResources().getLayout(menuRes);
            AttributeSet attrs = Xml.asAttributeSet(parser);

            parseMenu(parser, attrs, menu);
        } catch (XmlPullParserException e) {
            throw new InflateException("Error inflating menu XML", e);
        } catch (IOException e) {
            throw new InflateException("Error inflating menu XML", e);
        } finally {
            if (parser != null) parser.close();
        }
    
private voidparseMenu(org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.view.Menu menu)
Called internally to fill the given menu. If a sub menu is seen, it will call this recursively.

        MenuState menuState = new MenuState(menu);

        int eventType = parser.getEventType();
        String tagName;
        boolean lookingForEndOfUnknownTag = false;
        String unknownTagName = null;

        // This loop will skip to the menu start tag
        do {
            if (eventType == XmlPullParser.START_TAG) {
                tagName = parser.getName();
                if (tagName.equals(XML_MENU)) {
                    // Go to next tag
                    eventType = parser.next();
                    break;
                }

                throw new RuntimeException("Expecting menu, got " + tagName);
            }
            eventType = parser.next();
        } while (eventType != XmlPullParser.END_DOCUMENT);

        boolean reachedEndOfMenu = false;
        while (!reachedEndOfMenu) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (lookingForEndOfUnknownTag) {
                        break;
                    }

                    tagName = parser.getName();
                    if (tagName.equals(XML_GROUP)) {
                        menuState.readGroup(attrs);
                    } else if (tagName.equals(XML_ITEM)) {
                        menuState.readItem(attrs);
                    } else if (tagName.equals(XML_MENU)) {
                        // A menu start tag denotes a submenu for an item
                        SubMenu subMenu = menuState.addSubMenuItem();

                        // Parse the submenu into returned SubMenu
                        parseMenu(parser, attrs, subMenu);
                    } else {
                        lookingForEndOfUnknownTag = true;
                        unknownTagName = tagName;
                    }
                    break;

                case XmlPullParser.END_TAG:
                    tagName = parser.getName();
                    if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
                        lookingForEndOfUnknownTag = false;
                        unknownTagName = null;
                    } else if (tagName.equals(XML_GROUP)) {
                        menuState.resetGroup();
                    } else if (tagName.equals(XML_ITEM)) {
                        // Add the item if it hasn't been added (if the item was
                        // a submenu, it would have been added already)
                        if (!menuState.hasAddedItem()) {
                            if (menuState.itemActionProvider != null &&
                                    menuState.itemActionProvider.hasSubMenu()) {
                                menuState.addSubMenuItem();
                            } else {
                                menuState.addItem();
                            }
                        }
                    } else if (tagName.equals(XML_MENU)) {
                        reachedEndOfMenu = true;
                    }
                    break;

                case XmlPullParser.END_DOCUMENT:
                    throw new RuntimeException("Unexpected end of document");
            }

            eventType = parser.next();
        }