Skip to content

Fix memory leak in ToastUtils. Add a method to set the text size. #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions utilcode/src/main/java/com/blankj/utilcode/util/ToastUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

/**
* <pre>
* author: Blankj
Expand All @@ -30,16 +32,17 @@
*/
public final class ToastUtils {

private static final int COLOR_DEFAULT = 0xFEFFFFFF;
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
private static final int COLOR_DEFAULT = 0xFEFFFFFF;
private static final Handler HANDLER = new Handler(Looper.getMainLooper());

private static Toast sToast;
private static int sGravity = -1;
private static int sXOffset = -1;
private static int sYOffset = -1;
private static int sBgColor = COLOR_DEFAULT;
private static WeakReference<Toast> sToast;
private static int sGravity = -1;
private static int sXOffset = -1;
private static int sYOffset = -1;
private static int sBgColor = COLOR_DEFAULT;
private static int sBgResource = -1;
private static int sMsgColor = COLOR_DEFAULT;
private static int sMsgColor = COLOR_DEFAULT;
private static int sMsgTextSize = -1;

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

/**
* Set the text size of message.
*
* @param textSize The text size of message.
*/
public static void setMsgTextSize(final int textSize) {
sMsgTextSize = textSize;
}

/**
* Show the toast for a short period of time.
*
Expand Down Expand Up @@ -199,8 +211,9 @@ public static View showCustomLong(@LayoutRes final int layoutId) {
* Cancel the toast.
*/
public static void cancel() {
if (sToast != null) {
sToast.cancel();
Toast toast;
if (sToast != null && (toast = sToast.get()) != null) {
toast.cancel();
sToast = null;
}
}
Expand All @@ -222,8 +235,9 @@ private static void show(final CharSequence text, final int duration) {
@Override
public void run() {
cancel();
sToast = Toast.makeText(Utils.getTopActivityOrApp(), text, duration);
final TextView tvMessage = sToast.getView().findViewById(android.R.id.message);
Toast toast = Toast.makeText(Utils.getTopActivityOrApp(), text, duration);
sToast = new WeakReference<>(toast);
final TextView tvMessage = toast.getView().findViewById(android.R.id.message);
int msgColor = tvMessage.getCurrentTextColor();
//it solve the font of toast
TextViewCompat.setTextAppearance(tvMessage, android.R.style.TextAppearance);
Expand All @@ -232,11 +246,14 @@ public void run() {
} else {
tvMessage.setTextColor(msgColor);
}
if (sMsgTextSize != -1) {
tvMessage.setTextSize(sMsgTextSize);
}
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1) {
sToast.setGravity(sGravity, sXOffset, sYOffset);
toast.setGravity(sGravity, sXOffset, sYOffset);
}
setBg(tvMessage);
sToast.show();
setBg(toast, tvMessage);
toast.show();
}
});
}
Expand All @@ -246,20 +263,22 @@ private static void show(final View view, final int duration) {
@Override
public void run() {
cancel();
sToast = new Toast(Utils.getTopActivityOrApp());
sToast.setView(view);
sToast.setDuration(duration);
Toast toast = new Toast(Utils.getTopActivityOrApp());
sToast = new WeakReference<>(toast);

toast.setView(view);
toast.setDuration(duration);
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1) {
sToast.setGravity(sGravity, sXOffset, sYOffset);
toast.setGravity(sGravity, sXOffset, sYOffset);
}
setBg();
sToast.show();
setBg(toast);
toast.show();
}
});
}

private static void setBg() {
View toastView = sToast.getView();
private static void setBg(Toast toast) {
View toastView = toast.getView();
if (sBgResource != -1) {
toastView.setBackgroundResource(sBgResource);
} else if (sBgColor != COLOR_DEFAULT) {
Expand All @@ -274,8 +293,8 @@ private static void setBg() {
}
}

private static void setBg(final TextView tvMsg) {
View toastView = sToast.getView();
private static void setBg(final Toast toast, final TextView tvMsg) {
View toastView = toast.getView();
if (sBgResource != -1) {
toastView.setBackgroundResource(sBgResource);
tvMsg.setBackgroundColor(Color.TRANSPARENT);
Expand Down