背景
安卓开发中有时会遇到这样的场景,因为后台调用接口报错或者其他原因,导致toast不停的弹出来,体验很差。
解决方案写个工具类,如果toast对象存在,就复用,只是去改变里面显示的内容。
class Util {
private static Toast toast;
public static void showToast(Context context,
String content) {
if (toast == null) {
toast = Toast.makeText(context,
content,
Toast.LENGTH_SHORT);
} else {
toast.setText(content);
}
toast.show();
}
}
#参考资料 https://blog.csdn.net/qq_36946446/article/details/83506727