Skip to content

Commit f652fdf

Browse files
authored
Merge pull request Blankj#456 from LambertCoding/master
Fix memory leak in ToastUtils. Add a method to set the text size.
2 parents 64c72c5 + 3855044 commit f652fdf

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import android.widget.TextView;
2121
import android.widget.Toast;
2222

23+
import java.lang.ref.WeakReference;
24+
2325
/**
2426
* <pre>
2527
* author: Blankj
@@ -30,16 +32,17 @@
3032
*/
3133
public final class ToastUtils {
3234

33-
private static final int COLOR_DEFAULT = 0xFEFFFFFF;
34-
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
35+
private static final int COLOR_DEFAULT = 0xFEFFFFFF;
36+
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
3537

36-
private static Toast sToast;
37-
private static int sGravity = -1;
38-
private static int sXOffset = -1;
39-
private static int sYOffset = -1;
40-
private static int sBgColor = COLOR_DEFAULT;
38+
private static WeakReference<Toast> sToast;
39+
private static int sGravity = -1;
40+
private static int sXOffset = -1;
41+
private static int sYOffset = -1;
42+
private static int sBgColor = COLOR_DEFAULT;
4143
private static int sBgResource = -1;
42-
private static int sMsgColor = COLOR_DEFAULT;
44+
private static int sMsgColor = COLOR_DEFAULT;
45+
private static int sMsgTextSize = -1;
4346

4447
private ToastUtils() {
4548
throw new UnsupportedOperationException("u can't instantiate me...");
@@ -85,6 +88,15 @@ public static void setMsgColor(@ColorInt final int msgColor) {
8588
sMsgColor = msgColor;
8689
}
8790

91+
/**
92+
* Set the text size of message.
93+
*
94+
* @param textSize The text size of message.
95+
*/
96+
public static void setMsgTextSize(final int textSize) {
97+
sMsgTextSize = textSize;
98+
}
99+
88100
/**
89101
* Show the toast for a short period of time.
90102
*
@@ -199,8 +211,9 @@ public static View showCustomLong(@LayoutRes final int layoutId) {
199211
* Cancel the toast.
200212
*/
201213
public static void cancel() {
202-
if (sToast != null) {
203-
sToast.cancel();
214+
Toast toast;
215+
if (sToast != null && (toast = sToast.get()) != null) {
216+
toast.cancel();
204217
sToast = null;
205218
}
206219
}
@@ -222,8 +235,9 @@ private static void show(final CharSequence text, final int duration) {
222235
@Override
223236
public void run() {
224237
cancel();
225-
sToast = Toast.makeText(Utils.getTopActivityOrApp(), text, duration);
226-
final TextView tvMessage = sToast.getView().findViewById(android.R.id.message);
238+
Toast toast = Toast.makeText(Utils.getTopActivityOrApp(), text, duration);
239+
sToast = new WeakReference<>(toast);
240+
final TextView tvMessage = toast.getView().findViewById(android.R.id.message);
227241
int msgColor = tvMessage.getCurrentTextColor();
228242
//it solve the font of toast
229243
TextViewCompat.setTextAppearance(tvMessage, android.R.style.TextAppearance);
@@ -232,11 +246,14 @@ public void run() {
232246
} else {
233247
tvMessage.setTextColor(msgColor);
234248
}
249+
if (sMsgTextSize != -1) {
250+
tvMessage.setTextSize(sMsgTextSize);
251+
}
235252
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1) {
236-
sToast.setGravity(sGravity, sXOffset, sYOffset);
253+
toast.setGravity(sGravity, sXOffset, sYOffset);
237254
}
238-
setBg(tvMessage);
239-
sToast.show();
255+
setBg(toast, tvMessage);
256+
toast.show();
240257
}
241258
});
242259
}
@@ -246,20 +263,22 @@ private static void show(final View view, final int duration) {
246263
@Override
247264
public void run() {
248265
cancel();
249-
sToast = new Toast(Utils.getTopActivityOrApp());
250-
sToast.setView(view);
251-
sToast.setDuration(duration);
266+
Toast toast = new Toast(Utils.getTopActivityOrApp());
267+
sToast = new WeakReference<>(toast);
268+
269+
toast.setView(view);
270+
toast.setDuration(duration);
252271
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1) {
253-
sToast.setGravity(sGravity, sXOffset, sYOffset);
272+
toast.setGravity(sGravity, sXOffset, sYOffset);
254273
}
255-
setBg();
256-
sToast.show();
274+
setBg(toast);
275+
toast.show();
257276
}
258277
});
259278
}
260279

261-
private static void setBg() {
262-
View toastView = sToast.getView();
280+
private static void setBg(Toast toast) {
281+
View toastView = toast.getView();
263282
if (sBgResource != -1) {
264283
toastView.setBackgroundResource(sBgResource);
265284
} else if (sBgColor != COLOR_DEFAULT) {
@@ -274,8 +293,8 @@ private static void setBg() {
274293
}
275294
}
276295

277-
private static void setBg(final TextView tvMsg) {
278-
View toastView = sToast.getView();
296+
private static void setBg(final Toast toast, final TextView tvMsg) {
297+
View toastView = toast.getView();
279298
if (sBgResource != -1) {
280299
toastView.setBackgroundResource(sBgResource);
281300
tvMsg.setBackgroundColor(Color.TRANSPARENT);

0 commit comments

Comments
 (0)