diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2014-07-25 12:26:12 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2014-07-25 12:26:12 +0200 |
| commit | 4893141b7f53e7083f9d22933c5ba03e8e0378c8 (patch) | |
| tree | ab11948e105f7184a6cedcd8287904f102bbcd4b | |
| parent | 8fb22f5b8ea948b93a671fb9212709543b701d2e (diff) | |
| download | cgeo-4893141b7f53e7083f9d22933c5ba03e8e0378c8.zip cgeo-4893141b7f53e7083f9d22933c5ba03e8e0378c8.tar.gz cgeo-4893141b7f53e7083f9d22933c5ba03e8e0378c8.tar.bz2 | |
Allow toasts to be shown from any thread
| -rw-r--r-- | main/src/cgeo/geocaching/activity/ActivityMixin.java | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/main/src/cgeo/geocaching/activity/ActivityMixin.java b/main/src/cgeo/geocaching/activity/ActivityMixin.java index 769082d..b58d3ae 100644 --- a/main/src/cgeo/geocaching/activity/ActivityMixin.java +++ b/main/src/cgeo/geocaching/activity/ActivityMixin.java @@ -60,26 +60,47 @@ public final class ActivityMixin { return R.style.popup_dark; } + /** + * Show a long toast message to the user. This can be called from any thread. + * + * @param activity the activity the user is facing + * @param resId the message + */ public static void showToast(final Activity activity, final int resId) { ActivityMixin.showToast(activity, activity.getString(resId)); } - public static void showToast(final Activity activity, final String text) { + private static void postShowToast(final Activity activity, final String text, final int toastDuration) { if (StringUtils.isNotBlank(text)) { - Toast toast = Toast.makeText(activity, text, Toast.LENGTH_LONG); - - toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100); - toast.show(); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + final Toast toast = Toast.makeText(activity, text, toastDuration); + toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100); + toast.show(); + } + }); } } - public static void showShortToast(final Activity activity, final String text) { - if (StringUtils.isNotBlank(text)) { - Toast toast = Toast.makeText(activity, text, Toast.LENGTH_SHORT); + /** + * Show a long toast message to the user. This can be called from any thread. + * + * @param activity the activity the user is facing + * @param text the message + */ + public static void showToast(final Activity activity, final String text) { + postShowToast(activity, text, Toast.LENGTH_LONG); + } - toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100); - toast.show(); - } + /** + * Show a short toast message to the user. This can be called from any thread. + * + * @param activity the activity the user is facing + * @param text the message + */ + public static void showShortToast(final Activity activity, final String text) { + postShowToast(activity, text, Toast.LENGTH_SHORT); } public static void keepScreenOn(final Activity abstractActivity, boolean keepScreenOn) { |
