Datepicker Dialog Using Dialogfragment For Multiple Activities
Solution 1:
I ran into a similar problem. I wanted to use an AlertDialog Dialog Fragment in multiple activities.
Here is my DialogFragment which is made of an Alert Dialog. In newInstance I require the title and the activity. Title is going to be in string.xml. Activity is going to be 1 or 2 based on what I say when I create a new MyDialog. I pass both of those to the Bundle "args" using putInt.(You can also use putString if you are more comfortable with that. Then in the onCreateDialog, I pull both the title and the activity name from getArguments() and use the activity to decided in a switch which action to take. And of course based on which Activity I am trying to do the .doPositiveClick or .doNegativeClick, I have to cast which Activity to perform the function in. Using this method, I can make minimal changes to MyDialog class and use it in other Activities.
publicclassMyDialogextendsDialogFragment {
static MyDialog newInstance(int title, int activity) {
MyDialogdialog=newMyDialog();
Bundleargs=newBundle();
args.putInt("title", title);
args.putInt("activity", activity);
dialog.setArguments(args);
return dialog;
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog constructioninttitle= getArguments().getInt("title");
intactivity= getArguments().getInt("activity");
returnnewAlertDialog.Builder(getActivity())
.setTitle(title)
.setPositiveButton(R.string.yes,
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
switch (activity){
case1:
((MyActivity) getActivity()).doPositiveClick();
break;
case2:
((AnotherActivity) getActivity()).doPositiveClick();
break;
}
}
}
)
.setNegativeButton(R.string.cancel,
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
switch (activity) {
case1:
((MyActivity)getActivity()).doNegativeClick();
break;
case2:
((AnotherActivity)getActivity()).doNegativeClick();
break;
}
}
}
)
.create();
}
}
Here is where I call on my alert dialog. This can be used in two different Activities. "MyActivity" and "AnotherActivity"
voidshowDeleteDialog() {
DialogFragment newFragment = MyDialog.newInstance(
R.string.myDialogTitle, 1);
newFragment.show(getFragmentManager(), "dialog");
}
I hope this helps. Let me know if you need more clarification.
Solution 2:
am to late but i hope this answer will help some one in future : i do'it by passing DIALOG_ID argument by setArgument method for each activity and in dialogFragment check the DIALOG_ID value and set pickerDialog for each one ex: {
publicDialogonCreateDialog(Bundle savedInstanceState) {
if (dialog_id == 1) {
returnnewDatePickerDialog(getActivity(), ondateSet, year, month, day);
} else {
returnnewTimePickerDialog(getContext(), ontimeSet, hour, minute, false);
}
}
}
like this snippet example to under stand it more clear
Post a Comment for "Datepicker Dialog Using Dialogfragment For Multiple Activities"