Created
August 6, 2014 15:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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