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 interfce
    public interface SuperListener{
        //for example a confirm dialog
        void getDialogOk(View dialogView);
    }
    
    //in constructor, get listener instance from HostActivity
    public YourDialogFragment(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?"