也许有点晚了,但我也遇到了从非 FragmentActivity 调用 DialogFragment 的问题。同样将其设置为 FragmentActivity 只是为了一个简单的对话框是没有意义的。所以我的解决方案是制作一个带有回调无效的接口,以在活动中获取 DialogFragment 响应。对我来说,最好在对话调用附近获得响应,而不是在 onActivityResult 覆盖中的某个地方获得响应。
界面:
import android.os.Parcelable;
/**
* Created by TH on 03.11.2015.
*/
public interface OnDialogResponseListener extends Parcelable {
void onDialogResponse(int responseCode);
}
通用对话框类:
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:
* http://stackoverflow.com/questions/7977392/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();
}
}
用法是这样的:
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");
就这些。可能比简单对话框中的代码多一点,但它保持其值并在方向更改时显示。这就是我使用 DialogFragment 的目标。