My activity contains two fragments, instanciated with the same class.
Using the contextual menu, when I select a menu action from the second fragment/listview, logging the instance members shows that onContextItemSelected is called from the first fragment instance only:
public class LikedFragment extends Fragment {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMyInstanceId = getArguments().getString("fragmentId"); // "first" or "second"
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.my_fragment, container, false);
mAdapter = new MyAdapter(mActivity, MyQueries.getMyCursor(mMyInstanceId));
AbsListView listView = (AbsListView) mView.findViewById(android.R.id.list);
listView.setAdapter(mAdapter);
listView.setOnCreateContextMenuListener(this);
return mView;
}
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getActivity().getMenuInflater().inflate(R.menu.menu_item_liked, menu);
Log.d(TAG, mMyInstanceId); // OKAY: logs "second"
}
@Override public boolean onContextItemSelected(MenuItem item) {
Log.d(TAG, mMyInstanceId); // FAIL: logs "first"
/* ... */
}
}
How can I get the right instance in onContextItemSelected ?