Maybe a little bit late but I also had the problem to call a DialogFragment from a non FragmentActivity. Also making it a FragmentActivity just for a simple dialog would not make sense. So the solution for me was to make a interface with a callback void to get the DialogFragment response in the activity. For me it is bether to get the response near the dialog call rather than geting it somewhere in a onActivityResult override.
The interface:
import android.os.Parcelable;
/**
* Created by TH on 03.11.2015.
*/
public interface OnDialogResponseListener extends Parcelable {
void onDialogResponse(int responseCode);
}
The generic dialog class:
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
/**
* Code basisc found here:
* https://mcmap.net/q/27021/-android-dialogfragment-vs-dialog
* changed by TH on 03.11.2015.
*/
public class YesNoDialog extends DialogFragment
{
public static final String TITLE="title";
public static final String MESSAGE="message";
public static final String LISTENER="listener";
public static final String YES="yes";
public static final String NO="no";
public static final String ICON_ID="icon_id";
private OnDialogResponseListener mResponseListener;
public YesNoDialog()
{
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Bundle args = getArguments();
String title = args.getString(TITLE, "");
String message = args.getString(MESSAGE, "");
String yes = args.getString(YES, "");
String no = args.getString(NO, "");
int iconID=args.getInt(ICON_ID);
mResponseListener=args.getParcelable(LISTENER);
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(message)
.setIcon(iconID)
.setPositiveButton(yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mResponseListener != null) {
mResponseListener.onDialogResponse(Activity.RESULT_OK);
}
}
})
.setNegativeButton(no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mResponseListener != null) {
mResponseListener.onDialogResponse(Activity.RESULT_CANCELED);
}
}
})
.create();
}
}
And the usage would be like this:
OnDialogResponseListener onDialogResponseListener=new OnDialogResponseListener() {
@Override
public int describeContents() {
return 0; //Leave this as it is
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//Leave this as it is
}
@Override
public void onDialogResponse(int responseCode) {
//Do what you want with your dialog answers
if(responseCode== Activity.RESULT_OK){
Toast.makeText(MovementActivity.this,"OK pressed",Toast.LENGTH_LONG).show();
}else if(responseCode==Activity.RESULT_CANCELED){
Toast.makeText(MovementActivity.this,"CANCEL pressed",Toast.LENGTH_LONG).show();
}
}
};
YesNoDialog dialog = new YesNoDialog();
Bundle args = new Bundle();
args.putInt(YesNoDialog.ICON_ID, R.drawable.ic_action_add_light);
args.putString(YesNoDialog.YES, getString(R.string.action_add));
args.putString(YesNoDialog.NO, getString(R.string.action_cancel));
args.putString(YesNoDialog.TITLE, getString(R.string.dialog_title_add_item));
args.putString(YesNoDialog.MESSAGE, getString(R.string.dialog_message_add_item));
args.putParcelable(YesNoDialog.LISTENER, onDialogResponseListener);
dialog.setArguments(args);
dialog.show(getSupportFragmentManager(), "tag");
That is all. Maybe a little bit more code than in a simle Dialog but it keeps its values and shows on orientation change. That was my goal for using a DialogFragment.