Reputation: 2053
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;
}
}
Upvotes: 1
Views: 14811
Reputation: 6605
Here is how I called from my FragmentACtivity
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
ContactsNavi userPopUp = new ContactsNavi();
userPopUp.show(fragmentManager,"baglantilar");
Upvotes: 0
Reputation: 133560
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
Upvotes: 0
Reputation: 7061
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;
}
Upvotes: 4