1

I have set up my Dialog Fragment class successfully, and now I would like to call it from my main Fragment class I have set up.

I have tried using multiple code to call it, but I keep getting errors and crashes.

What would I need to put in my onClick to call my Dialog Fragment?

Thanks in advance!

Main Fragment Class:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);

    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


            //Here


        }

    });

    return v;
}

Dialog Fragment:

class MyDialogFragment extends DialogFragment {
Context mContext;

public MyDialogFragment() {
    mContext = getActivity();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            mContext);
    alertDialogBuilder.setTitle("Set Wallpaper?");
    alertDialogBuilder.setMessage("Are you sure?");
    // null should be your on click listener
    alertDialogBuilder.setPositiveButton("OK", null);
    alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    return alertDialogBuilder.create();
}

public static MyDialogFragment newInstance() {
    MyDialogFragment f = new MyDialogFragment();
    return f;
}

}
Jack
  • 2,043
  • 7
  • 40
  • 69
  • Use this code to show dialog fragment MyDialogFragment newFragment = MyDialogFragment.newInstance();     newFragment.show(getFragmentManager(), null); – Rajiv Ratan Oct 09 '13 at 10:06
  • jack what is the use of `public static MyDialogFragment newInstance()` if you have `MyDialogFragment dialog = new MyDialogFragment()`? – Raghunandan Oct 09 '13 at 10:23
  • possible duplicate of [Calling DialogFragment from Fragment](http://stackoverflow.com/questions/19252288/calling-dialogfragment-from-fragment) – Raghunandan Oct 09 '13 at 10:31
  • try this http://stackoverflow.com/a/38500468/3496570 – Zar E Ahmer Jul 21 '16 at 09:42

4 Answers4

4

Here the solution:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);

    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
                MyDialogFragment dialog = MyDialogFragment.newInstance();
            dialog.show(getActivity().getFragmentManager(), "MyDialogFragment");
        }

    });

    return v;
}
Diego Palomar
  • 6,958
  • 2
  • 31
  • 42
  • op has a static method newInstance() which returns the instance of the dialog fragment. SO why this `MyDialogFragment dialog = new MyDialogFragment()` – Raghunandan Oct 09 '13 at 10:23
2

Try the following

FragmentTransaction ft = ((FragmentActivity)getActivity()).getSupportFragmentManager().beginTransaction();
MyDialogFragment dialog = MydialogFragment.newInstance();
dialog.show(ft, "Tag");

Here is also a great TUTORIAL

A.S.
  • 4,574
  • 3
  • 26
  • 43
0

In your Dialog fragment you already have the below which returns the instance of dialog framgent.

public static MyDialogFragment newInstance() {
    MyDialogFragment f = new MyDialogFragment();
    return f;  
}

So try the below

DialogFragment newFragment = MyDialogFragment.newInstance();// call the static method
newFragment.show(getActivity().getFragmentManager(), "dialog");

Look at the docs there is an example

http://developer.android.com/reference/android/app/DialogFragment.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Here is how I called from my FragmentACtivity

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                ContactsNavi userPopUp = new ContactsNavi();
                userPopUp.show(fragmentManager,"baglantilar");
Samir
  • 6,405
  • 5
  • 39
  • 42