Skip to content Skip to sidebar Skip to footer

How To Call An Activity From A Dialogfragment In Android?

I want to call an activity from Dialogfragment, I have attached the code and logcat below for your reference on what I have tried.Kindly provide me your knowledge on it. Thank you.

Solution 1:

There are two ways to call from Fragment to Activity that hosts the Fragment:

  1. Simply casting to HostActivity

    ((HostActivity) getActivity()).methodInActivity();
    
  2. Use interface in Fragment as listener, HostActivity implements the listener:

    private SuperListener hostActivity;
    
    //In Fragment, define interfcepublicinterfaceSuperListener{
        //for example a confirm dialogvoidgetDialogOk(View dialogView);
    }
    
    //in constructor, get listener instance from HostActivitypublicYourDialogFragment(SuperListener hostActivity)
    {
       this.hostActivity = hostActivity;
    }
    
    //when `Ok` clicked
    hostActivity.getDialogOk(dialogView);
    

Hope this is clear.

Post a Comment for "How To Call An Activity From A Dialogfragment In Android?"