12

This is my sample code:

public class MainActivity extends Activity {

    Button buttonClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonClick = (Button) findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "Here is the Toast", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

When ever I click on the Button, the Toast message is displayed. That's fine.

My question is:

When I click multiple times on the button and then close the application, then the Toasts continue starting their work of displaying messages.

And I don't want that.

I want if the application is closed then Toasts should also stop displaying their messages.

Can anybody tell me what I have to do for this???

click_whir
  • 427
  • 5
  • 19
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
  • you may do this when closing the app: http://stackoverflow.com/questions/4395062/how-to-cancel-toast – aldo.roman.nurena Feb 11 '13 at 05:57
  • 1
    Possible duplicate of [Android cancel Toast when exiting the app and when toast is being shown](http://stackoverflow.com/questions/16098151/android-cancel-toast-when-exiting-the-app-and-when-toast-is-being-shown) – Richard Le Mesurier Aug 16 '16 at 20:58

5 Answers5

10

I want if the application is closed then Toast should also stop displaying the message.

In your case call cancel() to Toast object to cancel it within onDestroy() method.

Here is a similar example.

Updated!

I tested OP solution but no result.

.hide() and .cancel() method is available for Toast but seem they are not working. The solution is, you have to create your own custom view which acts like a Toast and then you can cancel all Toasts when the Activity finishes.

Community
  • 1
  • 1
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • Is it necessary to create custom view to cancel the Toast only. It seems not a good option as I think. – Narendra Pal Feb 11 '13 at 08:20
  • You don't have any other option. – RobinHood Feb 11 '13 at 08:45
  • @RobinHood Your first answer was correct so +1. You can `.cancel()` a `Toast` as can be seen in related post [here](http://stackoverflow.com/questions/16098151/android-cancel-toast-when-exiting-the-app-and-when-toast-is-being-shown). Code works great, been in production for years. – Richard Le Mesurier Aug 16 '16 at 20:59
4

Store a reference to your toast object. In your onDestroy, if the toast is not null then call cancel() on it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
4

You probably want to cancel the Toast whenever your app is not visible, so I would cancel it on the method 'onStop()'.

Here it goes:

public class MainActivity extends Activity {
    private Toast toast = null;
    Button buttonClick;

    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);
        buttonClick = (Button) findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                toast.setText("My toast!");
                toast.show();
            }
        });
    }

    @Override
    protected void onStop () {
        super.onStop();
        toast.cancel();
    }

}

Edit: Updated, it should work as OP intended now.

Juan Andrés Diana
  • 2,215
  • 3
  • 25
  • 36
  • You are testing pressing many times a button and then changing app? This saves the reference to the last Toast so that it cancels the last one. Perhaps you want to cancel previous toasts if they were still visible so that they dont keep stacking – Juan Andrés Diana Feb 11 '13 at 06:21
  • that the point! OP wants to cancel all toast when activity get closed not only the last one. – RobinHood Feb 11 '13 at 06:23
  • Well it should work now, but the toasts dont stack when the app is running. Most people would expect this behaviour anyway. – Juan Andrés Diana Feb 11 '13 at 06:25
  • Tested your updated answered its behave weird! its keep cancel first time but after few minutes it start showing, test your code, you will come to know exactly whats the outcomes of your code. – RobinHood Feb 11 '13 at 06:27
  • Explain what you mean please. – Juan Andrés Diana Feb 11 '13 at 06:29
  • Ok, I corrected it. You were right, I'm sorry. I tested the new version on a very similar code and it works fine. – Juan Andrés Diana Feb 11 '13 at 07:07
  • Also, you are right about the methods hide and cancel not working as we would expect, that's why I'm using setText. – Juan Andrés Diana Feb 11 '13 at 07:09
0

Try this,

You can cancel Toast showing using this code.

  final Toast toast = Toast.makeText(getApplicationContext(), "This message will    disappear     in half second", Toast.LENGTH_SHORT);
toast.show();

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           toast.cancel(); 
       }
}, 500);
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
-1

Try to use finish() in your OnCreate() function on some condition. Hope this will help you.

David Walschots
  • 12,279
  • 5
  • 36
  • 59
Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95