回答編集履歴

1

コード追加

2023/01/02 09:37

投稿

jimbe
jimbe

スコア12696

test CHANGED
@@ -2,3 +2,135 @@
2
2
 
3
3
  SampleDialogFragment クラスの使い方が、1つ目を出す時と2つ目を出す時で違うのが原因と思います。
4
4
  発生している例外は、「 listener が null だ」というものです。1つ目を出す際に行っていることと同じことを2つ目を出す際にもしなければならないのではないでしょうか。
5
+
6
+ ---
7
+ 以下は FragmentResultListener/FragmentResult によるものです。
8
+ DialogFragment は 2 つの異なるレイアウトを 1 つのクラスで扱う意味がありませんので 2 つに分けています。(適切な名前が分かりませんので 0/1 としています)
9
+ 構造的には Dialog0 から直に Dialog1 を表示するのでは無く、 Dialog0 のどのボタンでも一旦 SimpleFragment に戻って必要なら Dialog1 を表示するようにしています。
10
+
11
+ SampleFragment.java
12
+ ```java
13
+ import android.os.Bundle;
14
+ import android.view.*;
15
+ import android.widget.Button;
16
+
17
+ import androidx.annotation.*;
18
+ import androidx.fragment.app.*;
19
+
20
+ public class SampleFragment extends Fragment {
21
+ private static final String DIALOG_REQUEST_KEY = "dialog_request_key";
22
+
23
+ SampleFragment() {
24
+ super(R.layout.fragment_sample);
25
+ }
26
+
27
+ @Override
28
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
29
+ MainActivity activity = (MainActivity)requireActivity();
30
+ activity.setTitle("ダイアログコールバックサンプル");
31
+
32
+ FragmentManager fragmentManager = getChildFragmentManager();
33
+
34
+ Button button = view.findViewById(R.id.bt_show_dialog);
35
+ button.setText("ダイアログ表示する");
36
+ button.setOnClickListener(v -> SampleDialog0Fragment.newInstance(DIALOG_REQUEST_KEY).show(fragmentManager, null));
37
+
38
+ fragmentManager.setFragmentResultListener(DIALOG_REQUEST_KEY, getViewLifecycleOwner(), (requestKey, result) -> {
39
+ if(result.getBoolean("showDialog1", false)) {
40
+ SampleDialog1Fragment.newInstance(DIALOG_REQUEST_KEY).show(fragmentManager, null);
41
+ return;
42
+ }
43
+ String text = result.getString("text");
44
+ button.setText(text);
45
+ });
46
+ }
47
+ }
48
+ ```
49
+ SampleDialog0Fragment.java
50
+ ```java
51
+ import android.app.*;
52
+ import android.os.Bundle;
53
+ import android.view.*;
54
+ import android.widget.Button;
55
+
56
+ import androidx.fragment.app.DialogFragment;
57
+
58
+ public class SampleDialog0Fragment extends DialogFragment {
59
+ private static final String ARGS_REQUEST_KEY = "args_request_key";
60
+
61
+ public static SampleDialog0Fragment newInstance(String requestKey) {
62
+ SampleDialog0Fragment fragment = new SampleDialog0Fragment();
63
+ Bundle args = new Bundle();
64
+ args.putString(ARGS_REQUEST_KEY, requestKey);
65
+ fragment.setArguments(args);
66
+ return fragment;
67
+ }
68
+
69
+ @Override
70
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
71
+ String requestKey = getArguments().getString(ARGS_REQUEST_KEY);
72
+
73
+ View view = LayoutInflater.from(getContext()).inflate(R.layout.dialogfragment_sample, null, false);
74
+
75
+ Button closeButton = view.findViewById(R.id.setumei_bt_tojiru);
76
+ closeButton.setOnClickListener(v -> {
77
+ Bundle result = new Bundle();
78
+ result.putString("text", "dialog_0からきたよ");
79
+ getParentFragmentManager().setFragmentResult(requestKey, result);
80
+ dismiss();
81
+ });
82
+
83
+ Button itemButton = view.findViewById(R.id.setumei_bt_aitemu);
84
+ itemButton.setOnClickListener(v -> {
85
+ Bundle result = new Bundle();
86
+ result.putBoolean("showDialog1", true);
87
+ getParentFragmentManager().setFragmentResult(requestKey, result);
88
+ dismiss();
89
+ });
90
+
91
+ AlertDialog dialog = new AlertDialog.Builder(getContext())
92
+ .setView(view)
93
+ .create();
94
+ dialog.setCanceledOnTouchOutside(false); //ダイアログ以外の画面をタップしてもダイアログは消えない
95
+ return dialog;
96
+ }
97
+ }
98
+ ```
99
+ SampleDialog1Fragment.java
100
+ ```java
101
+ import android.app.*;
102
+ import android.os.Bundle;
103
+ import android.view.*;
104
+
105
+ import androidx.fragment.app.DialogFragment;
106
+
107
+ public class SampleDialog1Fragment extends DialogFragment {
108
+ private static final String ARGS_REQUEST_KEY = "args_request_key";
109
+
110
+ public static SampleDialog1Fragment newInstance(String requestKey) {
111
+ SampleDialog1Fragment fragment = new SampleDialog1Fragment();
112
+ Bundle args = new Bundle();
113
+ args.putString(ARGS_REQUEST_KEY, requestKey);
114
+ fragment.setArguments(args);
115
+ return fragment;
116
+ }
117
+
118
+ @Override
119
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
120
+ String requestKey = getArguments().getString(ARGS_REQUEST_KEY);
121
+
122
+ View view = LayoutInflater.from(getContext()).inflate(R.layout.dialogfragment_sample2, null, false);
123
+
124
+ AlertDialog dialog = new AlertDialog.Builder(getContext())
125
+ .setView(view)
126
+ .setPositiveButton("つかう", (d, i) -> {
127
+ Bundle result = new Bundle();
128
+ result.putString("text", "dialog_1からきたよ");
129
+ getParentFragmentManager().setFragmentResult(requestKey, result);
130
+ })
131
+ .create();
132
+ dialog.setCanceledOnTouchOutside(false); //ダイアログ以外の画面をタップしてもダイアログは消えない
133
+ return dialog;
134
+ }
135
+ }
136
+ ```