Android solves the problem that nested fragment cannot receive Oncreateoptionsmenu events _android

Source: Internet
Author: User

Objective

Nested level Two fragment cannot receive Oncreateoptionsmenu event, set Sethasoptionsmenu also no use.

Body

Supplementary Note:

If you manually call level two fragment through the cache fragment, there may be some puzzling problems, such as more menu not displayed.

Solution:

Add menu to the first level fragment, you can manually call level two of this method in the first level onoptionsitemselected to handle related events.

Sample code:

 @Override public
  void Oncreateoptionsmenu (Menu menu, Menuinflater inflater) {
    switch (getselectedposition ()) { Case
    1:
      inflater.inflate (r.menu.menu1, menu);
      break;
    Case 2:
      inflater.inflate (R.MENU.MENU2, menu);
      break;
    Case 0:
    default:
      inflater.inflate (R.MENU.MENU3, menu);
      break;
    }
  }

  @Override Public
  Boolean onoptionsitemselected (MenuItem item) {
    Fragment Fragment = getfragmentbyposition ( Getselectedposition ());
    if (fragment!= null)
      fragment.onoptionsitemselected (item);
    return super.onoptionsitemselected (item);
  }

Code Description:

The sample code is a level fragment,getfragmentbyposition that returns a level two fragment.

That's what Android solves fragment's inability to receive oncreateoptionsmenu events, hoping to help friends of Android developers solve similar problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.