Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jagandecapri/392759056a1a9cef27c8 to your computer and use it in GitHub Desktop.
Save jagandecapri/392759056a1a9cef27c8 to your computer and use it in GitHub Desktop.
Function to create Toast only if there is no Toast currently displayed . Avoids repeated toast creation.
/**
* Function to create Toast only if there is no Toast currently displayed
* Avoids repeated toast creation
*
* Sample usage/calling:
* showToast("lorem ipsum", Toast.LENGTH_SHORT);
*
* If needed call toast.cancel() in methods such as onPause() etc
* to cancel toast
**/
private Toast toast = null;
public void showToast(String msg, int duration){
if(toast != null){
if(toast.getView().isShown()){
toast.setText(msg);
}
else{
toast = Toast.makeText(this, msg, duration);
toast.show();
}
}
else{
toast = Toast.makeText(this, msg, duration);
toast.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment