1

I got a Fragment B in a Fragment A in a Activity. Works as expected. When clicking an item in Fragment B, I want to display a contextual menu bar.

I am working with ActionbarSherlock. What i've done this inside my Fragment B:

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.entry_list_context_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // make sure no item is selected when bar is shown
        adapter.clearSelection();
        adapter.notifyDataSetChanged();
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        Log.d("EntryList", "Item '" + item.getTitle()
                + "' clicked [onActionItemClicked()]");
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        adapter.clearSelection();
        adapter.notifyDataSetChanged();
        contextualMode = null;
    }
};
private ActionMode contextualMode;

@Override
public void onItemClick(AdapterView<?> parentView, View itemView,
        int index, long id) {
    DocumentEntity entry = (DocumentEntity) itemView.getTag();
    // something went wrong
    if (entry == null) {
        Log.e("EntryList", "Tag-Less item clicked [onItemClick()]");
        return;
    }

    if (contextualMode != null) {
        Log.d("EntryList",
                "contextualMode is not yet initialized [onItemClick()]");
        contextualMode = getSherlockActivity().startActionMode(
                mActionModeCallback);
    } else {
        Log.d("EntryList",
                "contextualMode already initialized [onItemClick()]");
    }

    entry.setSelected(!entry.isSelected());
    Log.d("EntryList", "entry.selected set to " + entry.isSelected()
            + " [onItemClick()]");
}

The selection works pretty good, but no contextual Actionbar is shown. The debug result is:

contextualMode already initialized [onItemClick()] entry.selected set to 'true' [onItemClick()]

There is no other position where contextualMode is set...

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Basic Coder
  • 10,882
  • 6
  • 42
  • 75
  • 2
    "I got a Fragment B in a Fragment A in a Activity. Works as expected." -- "expected" in this case means "does not work", as fragments inside of fragments is not supported, according to the engineer who wrote them: http://stackoverflow.com/questions/6847460/fragments-within-fragments/6847770#6847770 – CommonsWare May 23 '12 at 12:34

1 Answers1

1

I got a Fragment B in a Fragment A...

Android does not support embedding a fragment within another fragment. Sorry. This leads me to believe that your problem goes beyond the fact that your contextual ActionBar is not being shown. I suggest you clarify your original post.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250