Search code examples
javaandroidandroid-toast

toast message should be shown as many times as triggered but, it's only showing once


I have different buttons that trigger different toasts; all are triggered by onClick or onLongClick I initialized all toast messages in my OnCreate method. I decided to create one global toast so that everytime I want to display a toast I would say if(toast != null) toast.cancel() then toast = exampleToast; toast.show; So to avoid toasts being queued and to make it easy to cancel whatever toast being displayed before displaying the new one.

My problem is, in the situation where the same toast should appear as many times as I click its button, it only shows once.

private statice Toast toast is my globale toast; I tried making it static but it didn't solve the problem.

        private static Toast toast;        // copies different toast messages; easily referenced and cancelled
        private Toast backToast, deletedToast, setETimesToast, setRTimesToast, setLapsToast, savedToast,
        finishToast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Toast longClickToDeleteToast = Toast.makeText(MainActivity.this, R.string.long_press, Toast.LENGTH_SHORT);
        final Toast longClickToEditToast = Toast.makeText(MainActivity.this,"Long click to edit Title", Toast.LENGTH_SHORT);
        final Toast underDevToast = Toast.makeText(MainActivity.this, R.string.under_development, Toast.LENGTH_SHORT);
        finishToast = Toast.makeText(MainActivity.this, R.string.finished, Toast.LENGTH_SHORT);
        savedToast = Toast.makeText(MainActivity.this, R.string.saved , Toast.LENGTH_SHORT);
        setETimesToast = Toast.makeText(MainActivity.this, R.string.set_etime , Toast.LENGTH_SHORT);
        setRTimesToast = Toast.makeText(MainActivity.this, R.string.set_rtime, Toast.LENGTH_SHORT);
        setLapsToast = Toast.makeText(MainActivity.this, R.string.set_laps , Toast.LENGTH_SHORT);
        backToast = Toast.makeText(MainActivity.this, "Tap again to exit", Toast.LENGTH_SHORT);
        deletedToast = Toast.makeText(MainActivity.this, R.string.deleted, Toast.LENGTH_SHORT);

In the following code for example, three different toasts could show. However, if one toast has been triggered it won't show again until I trigger a different toast, it's driving me crazy!

public void onClick(View view){                
      if(toast != null) {
             toast.cancel();
             toast = null;
      }
      if(checkFields()){
             return;
      }
    private boolean checkFields() {
        final boolean b = true;
        toast = null;
        if (minutes == 0 && seconds == 0) {
            toast = setETimesToast;
            toast.show();
            return b;
        }
        if (restMin == 0 && restSec == 0) {
            toast = setRTimesToast;
            toast.show();
            return b;
        }
        if(laps == 0){
            toast = setLapsToast;
            toast.show();
            return b;
        }
        return false;
    }

Solution

  • I solved the problem by adding the condition && toast.getView().isShown() so that if (toast != null && toast.getView().isShown()) toast.cancel()