10
10
import androidx .annotation .StringRes ;
11
11
import androidx .appcompat .app .AlertDialog ;
12
12
import androidx .fragment .app .DialogFragment ;
13
+ import androidx .fragment .app .Fragment ;
14
+ import androidx .fragment .app .FragmentActivity ;
13
15
14
16
import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
15
17
import com .google .common .primitives .Booleans ;
@@ -36,18 +38,21 @@ public enum Type {
36
38
private List <Option <?>> options ;
37
39
38
40
@ NonNull
39
- public static <E extends Serializable > MultiOptionDialogFragment <E > newInstance (@ StringRes final int title ,
41
+ public static <E extends Serializable > MultiOptionDialogFragment <E > newInstance (final int requestCode ,
42
+ @ StringRes final int title ,
40
43
@ NonNull final ArrayList <Option <E >> options ) {
41
- return newInstance (title , 0 , 0 , options , Type .SINGLE );
44
+ return newInstance (requestCode , title , 0 , 0 , options , Type .SINGLE );
42
45
}
43
46
44
47
@ NonNull
45
- public static <E extends Serializable > MultiOptionDialogFragment <E > newInstance (@ StringRes final int title ,
48
+ public static <E extends Serializable > MultiOptionDialogFragment <E > newInstance (final int requestCode ,
49
+ @ StringRes final int title ,
46
50
@ StringRes final int positiveButtonText ,
47
51
@ StringRes final int negativeButtonText ,
48
52
@ NonNull final ArrayList <Option <E >> options ,
49
53
@ NonNull final Type type ) {
50
54
Bundle args = new Bundle ();
55
+ args .putInt ("requestCode" , requestCode );
51
56
args .putInt ("title" , title );
52
57
args .putInt ("positiveButtonText" , positiveButtonText );
53
58
args .putInt ("negativeButtonText" , negativeButtonText );
@@ -58,23 +63,44 @@ public static <E extends Serializable> MultiOptionDialogFragment<E> newInstance(
58
63
return fragment ;
59
64
}
60
65
66
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
61
67
@ Override
62
68
public void onAttach (@ NonNull final Context context ) {
63
69
super .onAttach (context );
64
70
this .context = context ;
71
+ final Fragment parentFragment = getParentFragment ();
72
+ if (parentFragment != null ) {
73
+ if (parentFragment instanceof MultiOptionDialogCallback ) {
74
+ callback = (MultiOptionDialogCallback ) parentFragment ;
75
+ }
76
+ if (parentFragment instanceof MultiOptionDialogSingleCallback ) {
77
+ singleCallback = (MultiOptionDialogSingleCallback ) parentFragment ;
78
+ }
79
+ return ;
80
+ }
81
+ final FragmentActivity fragmentActivity = getActivity ();
82
+ if (fragmentActivity instanceof MultiOptionDialogCallback ) {
83
+ callback = (MultiOptionDialogCallback ) fragmentActivity ;
84
+ }
85
+ if (fragmentActivity instanceof MultiOptionDialogSingleCallback ) {
86
+ singleCallback = (MultiOptionDialogSingleCallback ) fragmentActivity ;
87
+ }
65
88
}
66
89
67
90
@ NonNull
68
91
@ Override
69
92
public Dialog onCreateDialog (Bundle savedInstanceState ) {
70
93
final Bundle arguments = getArguments ();
71
94
int title = 0 ;
95
+ int rc = 0 ;
72
96
if (arguments != null ) {
97
+ rc = arguments .getInt ("requestCode" );
73
98
title = arguments .getInt ("title" );
74
99
type = (Type ) arguments .getSerializable ("type" );
75
100
}
101
+ final int requestCode = rc ;
76
102
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (context );
77
- if (title > 0 ) {
103
+ if (title != 0 ) {
78
104
builder .setTitle (title );
79
105
}
80
106
try {
@@ -89,11 +115,11 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
89
115
if (negativeButtonText > 0 ) {
90
116
builder .setNegativeButton (negativeButtonText , (dialog , which ) -> {
91
117
if (callback != null ) {
92
- callback .onCancel ();
118
+ callback .onCancel (requestCode );
93
119
return ;
94
120
}
95
121
if (singleCallback != null ) {
96
- singleCallback .onCancel ();
122
+ singleCallback .onCancel (requestCode );
97
123
}
98
124
});
99
125
}
@@ -113,7 +139,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
113
139
final Option <T > option = (Option <T >) options .get (position );
114
140
selected .add (option .value );
115
141
}
116
- callback .onMultipleSelect (selected );
142
+ callback .onMultipleSelect (requestCode , selected );
117
143
} catch (Exception e ) {
118
144
Log .e (TAG , "onCreateDialog: " , e );
119
145
}
@@ -133,7 +159,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
133
159
try {
134
160
final Option <?> option = options .get (which );
135
161
//noinspection unchecked
136
- callback .onCheckChange ((T ) option .value , isChecked );
162
+ callback .onCheckChange (requestCode , (T ) option .value , isChecked );
137
163
} catch (Exception e ) {
138
164
Log .e (TAG , "onCreateDialog: " , e );
139
165
}
@@ -157,7 +183,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
157
183
try {
158
184
final Option <?> option = options .get (which );
159
185
//noinspection unchecked
160
- callback .onCheckChange ((T ) option .value , true );
186
+ callback .onCheckChange (requestCode , (T ) option .value , true );
161
187
} catch (Exception e ) {
162
188
Log .e (TAG , "onCreateDialog: " , e );
163
189
}
@@ -168,7 +194,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
168
194
try {
169
195
final Option <?> option = options .get (which );
170
196
//noinspection unchecked
171
- singleCallback .onSelect ((T ) option .value );
197
+ singleCallback .onSelect (requestCode , (T ) option .value );
172
198
} catch (Exception e ) {
173
199
Log .e (TAG , "onCreateDialog: " , e );
174
200
}
@@ -190,19 +216,19 @@ public void setSingleCallback(final MultiOptionDialogSingleCallback<T> callback)
190
216
}
191
217
192
218
public interface MultiOptionDialogCallback <T > {
193
- void onSelect (T result );
219
+ void onSelect (int requestCode , T result );
194
220
195
- void onMultipleSelect (List <T > result );
221
+ void onMultipleSelect (int requestCode , List <T > result );
196
222
197
- void onCheckChange (T item , boolean isChecked );
223
+ void onCheckChange (int requestCode , T item , boolean isChecked );
198
224
199
- void onCancel ();
225
+ void onCancel (int requestCode );
200
226
}
201
227
202
228
public interface MultiOptionDialogSingleCallback <T > {
203
- void onSelect (T result );
229
+ void onSelect (int requestCode , T result );
204
230
205
- void onCancel ();
231
+ void onCancel (int requestCode );
206
232
}
207
233
208
234
public static class Option <T extends Serializable > {
0 commit comments