3
votes

When I used some nested fragments for my application, child fragment of the application cannot add new option menu by overriding onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) method(never hit this method).

Class hierarchy of application is as follow.

MainActivity.java(extends SherlockFragmentActivity) (MainActivity include the following child fragments)

  1. Fragment1.java(extends SherlockFragment)

  2. Fragment2.java(extends SherlockFragment)

(Fragment2 include FragmentTabHost for adding following child fragments)

 i. ChildFragment1.java(extends SherlockFragment)

 ii. ChildFragment2.java(extends SherlockFragment)

 iii. ChildFragment3.java(extends SherlockFragment)

I can add new OptionMenu in Fragment1, Fragment2 by adding setHasOptionsMenu(true) in onCreate(Bundle savedInstanceState) and by overriding onCreateOptionsMenu(Menu menu, MenuInflater menuInflater)

But I cannot add new OptionMenu in ChildFragment1, ChildFragment2, ChildFragment3 because onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) method of child fragment classes are never called.

2

2 Answers

1
votes

Now I can call the onCreateOptionsMenu of child fragments and can also call onOptionsItemSelected by following code.

Fragment2.java

protected FragmentTabHost mTabHost;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    super.onCreateOptionsMenu(menu, menuInflater);
    Fragment currentFragment = getChildFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
    if (currentFragment != null && currentFragment instanceof BaseTabsPagerChildFragment)
        ((BaseTabsPagerChildFragment) currentFragment).inflateOptionsMenu(menu, menuInflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (super.onOptionsItemSelected(item)) {
        return true;
    } else {
        Fragment currentFragment = getChildFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
        if (currentFragment != null && currentFragment instanceof BaseTabsPagerChildFragment) {
            return ((BaseTabsPagerChildFragment) currentFragment).onOptionsItemSelected(item);
        }
    }
    return false;
}

BaseTabsPagerChildFragment.java

abstract class BaseTabsPagerChildFragment extends BaseSherlockFragment {
    public abstract void inflateOptionsMenu(Menu menu, MenuInflater menuInflater);
}

ChildFragment1.java(extends BaseTabsPagerChildFragment.java)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
};

@Override
public void inflateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    onCreateOptionsMenu(menu, menuInflater);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    super.onCreateOptionsMenu(menu, menuInflater);
    menuInflater.inflate(R.menu.message_actionbar_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean handled = super.onOptionsItemSelected(item);
    if (handled) {
        return true;
    }
    int itemId_ = item.getItemId();
    if (itemId_ == R.id.menu_item_compose) {
        launchCompose();
        return true;
    }
    if (itemId_ == R.id.menu_item_delete) {
        doDelete();
        return true;
    }
    return false;
}

Thanks.

0
votes

You have to call the method manually. In your Fragment2 implement following

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    ChildFragment frag = (ChildFragment) getChildFragmentManager().findFragmentBy...;
    if (frag != null) frag.inflateOptionsMenu(menu, new MenuInflater(mActivity));
}

In your ChildFragment then have

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

public void inflateOptionsMenu(Menu menu, MenuInflater inflater) {
    onCreateOptionsMenu(menu, inflater);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.deal_detaisl, menu);
}

I found the solution in the chat folloing this question.