|
2 | 2 |
|
3 | 3 | package com.candycoded.alertconfirmdialog; |
4 | 4 |
|
| 5 | +import android.app.Activity; |
5 | 6 | import android.content.Context; |
6 | 7 | import android.app.AlertDialog; |
7 | 8 | import android.os.Bundle; |
|
10 | 11 |
|
11 | 12 | public class AndroidPlugin { |
12 | 13 |
|
13 | | - private Context context; |
| 14 | + private Activity activity; |
14 | 15 |
|
15 | 16 | public AndroidPlugin(Context context) { |
16 | | - this.context = context; |
| 17 | + this.activity = (Activity) context; |
17 | 18 | } |
18 | 19 |
|
19 | 20 | public void Alert(String title, String message, String okButtonLabel) { |
20 | 21 |
|
21 | | - AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); |
| 22 | + activity.runOnUiThread(() -> { |
22 | 23 |
|
23 | | - alertDialog.setTitle(title); |
24 | | - alertDialog.setMessage(message); |
25 | | - alertDialog.setPositiveButton(okButtonLabel, (dialog, which) -> { |
| 24 | + AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity); |
26 | 25 |
|
27 | | - dialog.dismiss(); |
| 26 | + alertDialog.setTitle(title); |
| 27 | + alertDialog.setMessage(message); |
| 28 | + alertDialog.setPositiveButton(okButtonLabel, (dialog, which) -> { |
28 | 29 |
|
29 | | - UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); |
| 30 | + dialog.dismiss(); |
| 31 | + |
| 32 | + UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); |
| 33 | + |
| 34 | + }); |
| 35 | + alertDialog.create(); |
| 36 | + alertDialog.show(); |
30 | 37 |
|
31 | 38 | }); |
32 | | - alertDialog.create(); |
33 | | - alertDialog.show(); |
34 | 39 |
|
35 | 40 | } |
36 | 41 |
|
37 | 42 | public void Confirm(String title, String message, String okButtonLabel, String cancelButtonLabel) { |
38 | 43 |
|
39 | | - AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); |
| 44 | + activity.runOnUiThread(() -> { |
40 | 45 |
|
41 | | - alertDialog.setTitle(title); |
42 | | - alertDialog.setMessage(message); |
43 | | - alertDialog.setPositiveButton(okButtonLabel, (dialog, which) -> { |
| 46 | + AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity); |
44 | 47 |
|
45 | | - dialog.dismiss(); |
| 48 | + alertDialog.setTitle(title); |
| 49 | + alertDialog.setMessage(message); |
| 50 | + alertDialog.setPositiveButton(okButtonLabel, (dialog, which) -> { |
46 | 51 |
|
47 | | - UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); |
| 52 | + dialog.dismiss(); |
48 | 53 |
|
49 | | - }); |
50 | | - alertDialog.setNegativeButton(cancelButtonLabel, (dialog, which) -> { |
| 54 | + UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); |
| 55 | + |
| 56 | + }); |
| 57 | + alertDialog.setNegativeButton(cancelButtonLabel, (dialog, which) -> { |
| 58 | + |
| 59 | + dialog.dismiss(); |
51 | 60 |
|
52 | | - dialog.dismiss(); |
| 61 | + UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "CANCEL"); |
53 | 62 |
|
54 | | - UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "CANCEL"); |
| 63 | + }); |
| 64 | + alertDialog.create(); |
| 65 | + alertDialog.show(); |
55 | 66 |
|
56 | 67 | }); |
57 | | - alertDialog.create(); |
58 | | - alertDialog.show(); |
59 | 68 |
|
60 | 69 | } |
61 | 70 |
|
|
0 commit comments