SoFunction
Updated on 2025-04-08

How to avoid displaying time overlays in Toast class

This article shares with you the method of avoiding display time superposition for your reference. The specific content is as follows

import ;
import ;
import ;
import ;
 
/**
  * Toast tool class
  * Created by user on 2016/12/22.
  */
 
public class ToastUtil {
 
  private static Toast toast = null;
  private static ToastUtil toastUtil = null;
 
  public ToastUtil(){}
 
  public synchronized static ToastUtil getInstance(){
    if(null == toastUtil){
      toastUtil = new ToastUtil();
    }
 
    return toastUtil;
  }
 
  public void showToast(Context context, String string){
 
    if(toast != null){
      ();
    }
      toast = (context, string,Toast.LENGTH_SHORT);
      ();
  }
 
  public void showToast(Fragment fragment, String string){
    showToast((),string);
  }
 
  public void showToast(Activity activity, String string){
    if(toast != null){
      ();
    }
    toast = (activity, string,Toast.LENGTH_SHORT);
    ();
  }
 
  public void showToastTest(Context context){
    if(toast != null){
      ();
    }
    toast = (context, "click",Toast.LENGTH_SHORT);
    ();
  }
 
  public void showToastTest(Fragment fragment){
    showToastTest(());
  }
 
  public void showToastTest(Activity activity){
    if(toast != null){
      ();
    }
    toast = (activity, "click",Toast.LENGTH_SHORT);
    ();
  }
}
 
    if(null == toastUtil){
      toastUtil = new ToastUtil();
    }
 
    return toastUtil;
  }
public void showToastInThread(Context context,String msg){
    ();
    if(toast != null){
      ();
    }
    toast = (context,msg,Toast.LENGTH_SHORT);
    ();
    ();
  }
  public void showToast(Context context, String string){
 
    if(toast != null){
      ();
    }
      toast = (context, string,Toast.LENGTH_SHORT);
      ();
  }
 
}

This is an encapsulated Toast tool class to avoid time overlay

How to use

().showToast(mContext,"test");

If executed in a thread, it must be in the following format

().showToastInThread(mContext,"str");

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.