I want to remove stacked Android Toasts after the user goes to another Fragment. I have Fragments that are stacked and in every Fragment, I have two buttons which triggers different Toast message. When operations of a Fragment is done and the user navigates to another Fragment or press back button Toasts are kept showing. This mainly happens when the user clicks buttons too fast and force Toasts to stack.
Or when I instantiate global Toast objects and call cancel() than both of toasts stop from showing in that lifecycle of a fragment no matter how many times the user tap button.
toast1 = new Toast(getContext());
toast2 = new Toast(getContext());
showFirstToast(toast1).show();
showSecondToast(toast2).show();
private Toast showFirstToast(Toast toast){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout_correct, (ViewGroup)
getActivity().findViewById(R.id.toast_layout));
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
return toast;
}