|
1 | 1 | package io.agora.api.example.common; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.Handler; |
| 6 | +import android.os.Looper; |
| 7 | +import android.widget.Toast; |
4 | 8 |
|
| 9 | +import androidx.annotation.Nullable; |
5 | 10 | import androidx.appcompat.app.AlertDialog; |
6 | 11 | import androidx.fragment.app.Fragment; |
7 | 12 |
|
8 | | -public class BaseFragment extends Fragment { |
9 | | - protected void showAlert(String message) { |
| 13 | +public class BaseFragment extends Fragment |
| 14 | +{ |
| 15 | + protected Handler handler; |
| 16 | + |
| 17 | + @Override |
| 18 | + public void onCreate(@Nullable Bundle savedInstanceState) |
| 19 | + { |
| 20 | + super.onCreate(savedInstanceState); |
| 21 | + handler = new Handler(Looper.getMainLooper()); |
| 22 | + } |
| 23 | + |
| 24 | + protected void showAlert(String message) |
| 25 | + { |
10 | 26 | Context context = getContext(); |
11 | 27 | if (context == null) return; |
12 | 28 |
|
13 | 29 | new AlertDialog.Builder(context).setTitle("Tips").setMessage(message) |
14 | 30 | .setPositiveButton("OK", (dialog, which) -> dialog.dismiss()) |
15 | 31 | .show(); |
16 | 32 | } |
| 33 | + |
| 34 | + protected final void showLongToast(final String msg) |
| 35 | + { |
| 36 | + handler.post(new Runnable() |
| 37 | + { |
| 38 | + @Override |
| 39 | + public void run() |
| 40 | + { |
| 41 | + if (BaseFragment.this == null || getContext() == null) |
| 42 | + {return;} |
| 43 | + Toast.makeText(getContext().getApplicationContext(), msg, Toast.LENGTH_LONG).show(); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
17 | 47 | } |
0 commit comments