4

In my application , on a button click I am creating a Toast as -

Toast.makeText(context,"Please Enter Username",Toast.LENGTH_SHORT).show();

But when someone clicks on the button 5-6 times and closes the application, or goes on another screen, it still keeps on showing the Toast for sometime on another screen also. I have seen many solutions for the same.

I have tried -

toast = Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT);
        toast.show();

and have cancelled this toast-

onPause(){

if(toast!=null){
toast.cancel();

}

and same on onDestroy()

I want that when anyone clicks on the button 5-6 times and goes out of the app or that activity, the toast message should disappear. Or suggest any alternate to solve the same.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

3 Answers3

16

But it gives me a force close as - Toast was never called by using Toast.makeText();

You can make a field variable and method to only display one Toast at a time:

Toast toast;

public void displayToast(String message) {
    if(toast != null)
        toast.cancel();
    toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
    toast.show();
}

And in onPause() cancel any existing Toast when you exit:

protected void onPause() {
    if(toast != null)
        toast.cancel();
    super.onPause();
}

Now whenever you want to display a Toast, simply call:

displayToast("Please Enter Username");
Sam
  • 86,580
  • 20
  • 181
  • 179
  • I have now tried as u have suggested, but Toast is not getting cleared. Force Close problem is resolved. But toast is not cancelling when I exit from the app or go to any another activity. Please help – Gaurav Arora Jan 13 '13 at 05:03
  • See I have upvoted you on other posts also. I will accept your answer as well, but if you can solve my other problem also bro. – Gaurav Arora Jan 13 '13 at 05:42
  • You don't have to upvote my other answers, in fact Stack Overflow automatically catches and removes "robo-voting". I edited my answer so you have the chance to undo your downvote, if you want. – Sam Jan 13 '13 at 05:46
  • @Sam AWESEOME tutorials! Thanks – Si8 Aug 16 '13 at 15:17
1

Try this :

Toast mToast;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
      }

    @Override
    protected void onPause() {
        mToast.cancel();
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void abc(View c) {

        mToast.cancel();
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setText("This is hi" + (i++));
        mToast.show();

    }

instead of using 'Toast.makeText' use 'mToast.setText("")' this way you will achieve your desired output.

Nimish Choudhary
  • 2,048
  • 18
  • 17
0

I had the same problem. The way I found to solve it was by declaring my Toast before the button method, and canceling and recreating the toast everytime the button is pressed. This way if you press it multiple times you will kill the previous instance of the Toast, and reassign a new Toast to your variable

var toast: Toast = Toast.makeText(this, "your text", Toast.LENGTH_SHORT)

rollButton.setOnClickListener {
        toast.cancel()
        toast = Toast.makeText(this, "your text", Toast.LENGTH_SHORT)
        toast.show()