-1

I have a Fragment A which shows a MenuItem Mi in the toolbar. On clicking Mi, I am showing a DialogFragment Df to the user to set a value V.

I am passing this value to fragment A by implementing a callback listener interface. Once the value is set, I want to hide Mi from toolbar menu of fragment A.

I wanted to handle this inside onPause() and onResume() of fragment A, but showing a DialogFragment doesn't change the lifecycle of fragment. I was wondering how to approach this problem.

How can I achieve this thing?

MSS
  • 3,306
  • 1
  • 19
  • 50

1 Answers1

0

I did this using a callback listener in my fragment A which listens to DialogFragment Df. Once the value V is set, this callback method in A is invoked. Inside this method, I am using V and setting a flag to indicate V has been set and then calling invalidateOptionsMenu().

Refer this for implementing your own callback. How to send data from DialogFragment to a Fragment?

public void myCallback(int V){
  //Use V according to my logic
   vIsSet = Boolean.TRUE;
   getActivity().invalidateOptionsMenu();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    super.onCreateOptionsMenu(menu,inflater);
    if(vIsSet) {
        menu.removeItem(MENU_ITEM_ID);//item id of Mi
    }
}
MSS
  • 3,306
  • 1
  • 19
  • 50