2

In my app I'm having a class which extends SherlockFragmentActivity. Within that Activity, I'm loading four classes which extends SherlockFragment which pretends as Tabs with the help of ActionbarSherlock and PagerSlidingTabStrip.

The classes which extends SherlockFragment has setHasOptionsMenu(true); inside onCreate() method. Even having that, when run the application, it won't call onCreateOptionsMenu() method.

I have read these similar questions (Q1, Q2) in SO and didn't get any help.

What can be the reason for that?


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

    setHasOptionsMenu(true);

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.record, menu);
    super.onCreateOptionsMenu(menu, inflater);
    Log.i(TAG, "* onCreateOptionsMenu");
}

UPDATE:

I think the reason is, I'm using SherlockFragment inside another SherlockFragment. Means in PageSlidingTabStripFragment.java has following method inside MyPagerAdapter class.

@Override
public SherlockFragment getItem(int position) {
    return SuperAwesomeCardFragment.newInstance(position);
}

What I'm doing is, I'm calling some other SherlockFragments from there like below.

@Override
public SherlockFragment getItem(int position) {
    Log.i(TAG, "* getItem");

    switch (position) {
        case 0:
            return new InnerSherlockFragmentOne();
        case 1:
            return new InnerSherlockFragmentTwo();
        case 2:
            return new InnerSherlockFragmentThree();
        case 3:
            return new InnerSherlockFragmentFour();
        default:
            return null;
    }
}

What is not calling is onCreateOptionsMenu(Menu menu, MenuInflater inflater) inside those InnerSherLockFragments.

Community
  • 1
  • 1
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • Maybe you forgot to add `menu.clear()` before inflating the menu. – Naddy Nov 19 '13 at 09:52
  • @Naddy No... I'm telling that `onCreateOptionsMenu()` not even call. If I put a `Log` message inside that method as first line, it won't print. – AnujAroshA Nov 19 '13 at 10:03
  • @Naddy added to the question now – AnujAroshA Nov 19 '13 at 10:39
  • I have implemented the same code and it's working for me. The only difference being that I have created the **MenuItems** programmatically. Are you using **MapFragment**?? – Naddy Nov 19 '13 at 12:54
  • @Naddy I'm not using MapFragment. Me too can create `MenuItems` programmatically. But to create `MenuItems` programatically we need to fire `onCreateOptionsMenu()` inside Fragment. That is the place not happening to me. You may have implemented the `MenuItems` inside `SherlockFragmentActivity` not `SherlockFragment` OR you may not creating different `MenuItems` for different Fragments. By the way, thanks for sharing your experience. – AnujAroshA Nov 19 '13 at 13:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41514/discussion-between-anujarosha-and-naddy) – AnujAroshA Nov 20 '13 at 08:58
  • This is not the solution. But I achieved this by removing ActionbarSherlock and using only PageSlidingTabStrip. – AnujAroshA Nov 26 '13 at 06:28

3 Answers3

4

The imports should be:

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

and not these of android OS!

Also try calling super method like:

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

    Log.i(TAG, "* onCreateOptionsMenu");
}
madlymad
  • 6,367
  • 6
  • 37
  • 68
  • @AnujAroshA are you sure that the activity on does not disable the menu? add methods also to the activity to test if it functions... – madlymad Nov 19 '13 at 10:51
  • Actually, if I'm creating Menu items in `onCreateOptionsMenu()` inside my Activity class which extends `SherlockFragmentActivity` it will display in the Menu. But then I'm not able to change Menu items according to the Fragment changes. That's why I'm trying to implement Menu items inside each Fragment Activity. – AnujAroshA Nov 19 '13 at 10:59
2

I had a same issue recently. And my code is very similar with yours that I use Fragment in PagerSlidingTabStrip...

My solution is something like this.

I don't do anything in my fragment but fix the source from PageSlidingTabStripFragment.java

PageSlidingTabStripFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    adapter = new MainPagerAdapter(getChildFragmentManager());
    setHasOptionsMenu(true);
setRetainInstance(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.e("test", "Fragment onCreateOptionsMenu");
    int position = pager.getCurrentItem();

    if (position == 4) {
        inflater.inflate(R.menu.fragment_report_menu, menu);
    }

    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.board_list_menu_new:
            ReportFragment fragment = (ReportFragment) adapter.getItem(pager.getCurrentItem());
            fragment.onMyOptionItemSelected();
            break;
    }
    return super.onOptionsItemSelected(item);
}

And that all.

This is not very clean solution and should be fixed in library. :)

Thanks, Wooram

Wooram Jung
  • 228
  • 2
  • 10
1

This is the exact code I have written and it works fine.

import android.os.Bundle;
import android.util.Log;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class First extends SherlockFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setHasOptionsMenu(true);

        super.onCreate(savedInstanceState);
        Log.i("TAG", "* created");
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        menu.add("abcd").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add("efgh").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        Log.i("TAG", "* onCreateOptionsMenu");
    }
}
gotwo
  • 663
  • 8
  • 16
Naddy
  • 2,664
  • 6
  • 25
  • 38