DialogFragmentからActivityに選択した結果を返すサンプルコード
選択肢とかを表示するのに、DialogFragmentを使うときがあると思います。
で、ActivityからDialogFragmentを表示させて、Activityに戻った時、
onActivityResult(int requestCode, int resultCode, Intent data)
で取得できそうですよね。
しかし、取得できません。
「なんやて工藤!!」
と驚きますが、本当です。Fragment→DialogFragmentへの遷移なら、onActivityResultが使えます。
「せやかて工藤…!!」
残念ながら、Interfaceを作って自分で実装するしかありません。
下記の回答の3番目を参考にして、次のように作りました。
https://stackoverflow.com/questions/10905312/receive-result-from-dialogfragment
//Activityでinterfaceをimplementsする public class SettingAlarmActivity extends UtilsFragmentActivity implements OnClickListener, DaySelectDialogFragment.InterfaceCommunicator { public static final int DAY_SELECT_REQUEST = 777; //中略 @Override public void onClick(View v) { switch (v.getId()) { case R.id.day_of_week: String type = "ON"; mAlarm.setType(type); DaySelectDialogFragment day_select_dialog = new DaySelectDialogFragment(); day_select_dialog.show(m_fm_manager, TAG); break; } @Override public void sendRequestCode(int requestCode, Intent data) { if (requestCode == DAY_SELECT_REQUEST) { // Make sure the request was successful mAlarm = data.getExtras().getParcelable("alarm"); String type = data.getExtras().getString("type"); if (type.equals("OFF")) { mSelectedItemsOff = mAlarm.getListAutoOffDay(); alarmHelper.displayOffDays(context, mSelectedItemsOff, off_sunday, off_monday, off_tuesday, off_wednesday, off_thursday, off_friday, off_saturday, no_days_select_off); } } }
//DialogFragmentでインターフェースを作成 public class DaySelectDialogFragment extends DialogFragment { private String type; public InterfaceCommunicator interfaceCommunicator; /** * Activityに結果を返すために、作ったインターフェース */ public interface InterfaceCommunicator { void sendRequestCode(int code, Intent intent); } /** * Create a new instance of MyDialogFragment, providing "num" * as an argument. */ static DaySelectDialogFragment newInstance(Alarm alarm) { DaySelectDialogFragment f = new DaySelectDialogFragment(); return f; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final SettingAlarmActivity settingAlarmActivity = (SettingAlarmActivity)getActivity(); type = fragmentAlarm.getType(); this.interfaceCommunicator = settingAlarmActivity; AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Set the dialog title builder.setTitle(R.string.day_of_week) .setMultiChoiceItems(R.array.dayOfWeeklist, selected_days, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //中略 } }) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Intent intent = new Intent(); intent.putExtra("alarm" , fragmentAlarm); intent.putExtra("type" , type); SettingAlarmHelper alarmHelper = new SettingAlarmHelper(getActivity()); alarmHelper.addAlarmAction(getActivity(), alarmHelper); interfaceCommunicator.sendRequestCode(settingAlarmActivity.DAY_SELECT_REQUEST, intent); dismiss(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); return builder.create(); } }