4
4
import android .os .Bundle ;
5
5
import android .os .Handler ;
6
6
import android .os .Looper ;
7
- import android .view .Menu ;
7
+ import android .text .TextUtils ;
8
+ import android .view .View ;
8
9
import android .widget .Toast ;
9
10
11
+ import androidx .activity .OnBackPressedCallback ;
10
12
import androidx .annotation .NonNull ;
11
13
import androidx .annotation .Nullable ;
12
14
import androidx .appcompat .app .AlertDialog ;
13
15
import androidx .fragment .app .Fragment ;
16
+ import androidx .navigation .Navigation ;
14
17
15
- import io .agora .api .example .R ;
16
-
17
- public class BaseFragment extends Fragment
18
- {
18
+ /**
19
+ * The type Base fragment.
20
+ */
21
+ public class BaseFragment extends Fragment {
22
+ /**
23
+ * The Handler.
24
+ */
19
25
protected Handler handler ;
20
26
private AlertDialog mAlertDialog ;
27
+ private String mAlertMessage ;
28
+ private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback (false ) {
29
+ @ Override
30
+ public void handleOnBackPressed () {
31
+ onBackPressed ();
32
+ }
33
+ };
21
34
22
35
@ Override
23
- public void onCreate (@ Nullable Bundle savedInstanceState )
24
- {
36
+ public void onCreate (@ Nullable Bundle savedInstanceState ) {
25
37
super .onCreate (savedInstanceState );
26
38
handler = new Handler (Looper .getMainLooper ());
39
+ requireActivity ().getOnBackPressedDispatcher ().addCallback (onBackPressedCallback );
40
+ }
41
+
42
+ @ Override
43
+ public void onAttach (@ NonNull Context context ) {
44
+ super .onAttach (context );
45
+ onBackPressedCallback .setEnabled (true );
46
+ }
47
+
48
+ @ Override
49
+ public void onDetach () {
50
+ super .onDetach ();
51
+ onBackPressedCallback .setEnabled (false );
27
52
}
28
53
54
+ /**
55
+ * Show alert.
56
+ *
57
+ * @param message the message
58
+ */
29
59
protected void showAlert (String message ) {
60
+ this .showAlert (message , true );
61
+ }
30
62
63
+ /**
64
+ * Show alert.
65
+ *
66
+ * @param message the message
67
+ * @param showRepeatMsg the show repeat msg
68
+ */
69
+ protected void showAlert (String message , boolean showRepeatMsg ) {
31
70
runOnUIThread (() -> {
32
71
Context context = getContext ();
33
- if (context == null ){
72
+ if (context == null ) {
34
73
return ;
35
74
}
36
75
if (mAlertDialog == null ) {
37
76
mAlertDialog = new AlertDialog .Builder (context ).setTitle ("Tips" )
38
77
.setPositiveButton ("OK" , (dialog , which ) -> dialog .dismiss ())
39
78
.create ();
40
79
}
80
+ if (!showRepeatMsg && !TextUtils .isEmpty (mAlertMessage ) && mAlertMessage .equals (message )) {
81
+ return ;
82
+ }
83
+ mAlertMessage = message ;
41
84
mAlertDialog .setMessage (message );
42
85
mAlertDialog .show ();
43
86
});
44
87
}
45
88
46
- protected final void showLongToast (final String msg )
47
- {
89
+ /**
90
+ * Reset alert.
91
+ */
92
+ protected void resetAlert () {
93
+ runOnUIThread (() -> mAlertMessage = "" );
94
+ }
95
+
96
+ /**
97
+ * Show long toast.
98
+ *
99
+ * @param msg the msg
100
+ */
101
+ protected final void showLongToast (final String msg ) {
48
102
runOnUIThread (() -> {
49
103
Context context = getContext ();
50
- if (context == null ){
104
+ if (context == null ) {
51
105
return ;
52
106
}
53
107
Toast .makeText (context , msg , Toast .LENGTH_LONG ).show ();
54
108
});
55
109
}
56
110
57
- protected final void showShortToast (final String msg )
58
- {
111
+ /**
112
+ * Show short toast.
113
+ *
114
+ * @param msg the msg
115
+ */
116
+ protected final void showShortToast (final String msg ) {
59
117
runOnUIThread (() -> {
60
118
Context context = getContext ();
61
- if (context == null ){
119
+ if (context == null ) {
62
120
return ;
63
121
}
64
122
Toast .makeText (context , msg , Toast .LENGTH_SHORT ).show ();
65
123
});
66
124
}
67
125
68
- protected final void runOnUIThread (Runnable runnable ){
126
+ /**
127
+ * Run on ui thread.
128
+ *
129
+ * @param runnable the runnable
130
+ */
131
+ protected final void runOnUIThread (Runnable runnable ) {
69
132
this .runOnUIThread (runnable , 0 );
70
133
}
71
134
72
- protected final void runOnUIThread (Runnable runnable , long delay ){
73
- if (handler != null && runnable != null && getContext () != null ){
135
+ /**
136
+ * Run on ui thread.
137
+ *
138
+ * @param runnable the runnable
139
+ * @param delay the delay
140
+ */
141
+ protected final void runOnUIThread (Runnable runnable , long delay ) {
142
+ if (handler != null && runnable != null && getContext () != null ) {
74
143
if (delay <= 0 && handler .getLooper ().getThread () == Thread .currentThread ()) {
75
144
runnable .run ();
76
- }else {
145
+ } else {
77
146
handler .postDelayed (() -> {
78
- if (getContext () != null ){
147
+ if (getContext () != null ) {
79
148
runnable .run ();
80
149
}
81
150
}, delay );
@@ -87,15 +156,19 @@ protected final void runOnUIThread(Runnable runnable, long delay){
87
156
public void onDestroy () {
88
157
super .onDestroy ();
89
158
handler .removeCallbacksAndMessages (null );
90
- if (mAlertDialog != null ){
159
+ if (mAlertDialog != null ) {
91
160
mAlertDialog .dismiss ();
92
161
mAlertDialog = null ;
93
162
}
94
163
}
95
164
96
- @ Override
97
- public void onPrepareOptionsMenu (@ NonNull Menu menu ) {
98
- super .onPrepareOptionsMenu (menu );
99
- menu .setGroupVisible (R .id .main_setting_group , false );
165
+ /**
166
+ * On back pressed.
167
+ */
168
+ protected void onBackPressed () {
169
+ View view = getView ();
170
+ if (view != null ) {
171
+ Navigation .findNavController (view ).navigateUp ();
172
+ }
100
173
}
101
174
}
0 commit comments