先说一下解决方法,就是整个Application或者同一个界面用同一个toast,当有需要显示的直接调用setText()和show()方法,之前的显示就会被覆盖掉。如果直接用Toast.makeText().show();事实上是新new 一个Toast在显示
public class ToastManager {
private static ToastManager manager;
private Context context;
private Toast toast;
public ToastManager() {
init();
}
public static ToastManager getManager() {
if (manager == null)
synchronized (ToastManager.class) {
manager = new ToastManager();
}
return manager;
}
private void init() {
context = AppApplication.getInstance();
toast = Toast.makeText(context, null, Toast.LENGTH_SHORT);
}
public void show(int resId) {
show(resId,Toast.LENGTH_SHORT);
}
public void show(String hint) {
show(hint,Toast.LENGTH_SHORT);
}
public void show(int resId, int duration) {
toast.setDuration(duration);
toast.setText(resId);
toast.show();
}
public void show(String hint, int duration) {
toast.setDuration(duration);
toast.setText(hint);
toast.show();
}
public void onDestroy() {
toast.cancel();
toast = null;
manager = null;
}
}