I have seen a number of questions on cancelling toast. None of them is working.
I have a custom Toast. The code for that is all but one line same as http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView
The difference is as follows.
The toast variable is defined as a class variable
The entire java code is written in a method.
In the start of this method, I have added the following line to cancel the toast.
if (toast!=null){
toast.cancel();
}
The method is called when user selects (onClick) the view/layout. The issue is when the user selects few times, the toast will get queued up (the toast.cancel is not working).
Any solutions?
[update]
I tried making toast object a static variable. Still dont work.
I suffered from same issue (custom toast queuing up) and found a solution. It worked fine in my case.
Having custom toast object initially set to null.
If this is null, create new custom toast object with "new".
As far as you are in same activity, don't "new" to create new object. Instead, use that object. Since setText() won't work in this case, use setView() as you do with your custom toast.
With this way show(), cancel(), show(), cancel() worked exactly as I expect. No delay, no queuing.
Hope this helps.
In the end, I created a Custom Dialog so that the user is blocked from doing anything else (and avoids multiple toasts popping up). Added a onClick Listener Event to close the dialog when user clicks the same.
Sad that Toast.cancel() doesn't work.
in above code toast.setDuration(Toast.LENGTH_LONG);
in that u use toast.setDuration(Toast.LENGTH_SHORT);
or se the particular time.Toast is cancelled automatically.we can't cancel it
Use this code for custom text:
LayoutInflater mInflater=LayoutInflater.from(context);
View view=mInflater.inflate(R.layout.tost_layout,null);
Toast toast=new Toast(this);
toast.setView(view);
toast.show();
Related
I am getting an error as Null pointer exception and unable to instantiate activity when I am creating a toast in activity constructor.I want to know the reason why toasts are working in onCreate method but not in activity constructor
The OS is responsible for constructing your Activity classes. This is because it needs to perform some setup, including providing the Activity a proper base Context. Without this, you cannot make Toasts.
In general, you should avoid doing anything in the constructor of an Activity (and you should definitely not be making instances of them yourself using new).
Usually a Toast appears as a feedback to user input (say a button click) or when some external event is registerd (say a network error happend or new incoming data is available).
However, to me it sounds as though you want to show a Toast right after opening your activity, is that correct? Then you should put it in the onCreate method of your activity. An example Toast is made like this
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG).show();
You might also consider putting this call in the onResume method. I recommend getting familiar with activity lifecycles.
Instead of using Toasts you might also want to check out Snackbars.
Inside onCreate of Application class, I set its instance to a static field, then show all application Toasts through this context. All works good except one thing, in some places a Toast can be shown before first activity can even appear but Toast never appears or sometimes just flashes. I think its because Activity not shown or drawn yet ? Or I'm missing something.
Edit:
More like showing toast from onCreate of Application class
Edit 2 :
public class TestApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Test from App", Toast.LENGTH_LONG).show();
}
}
If you want to show a Toast without the application even started manually by the user you can register a BroadcastReceiver which listens to the BOOT_COMPLETED system broadcast and then start a Service which will handle your Toasts.
You'll find many examples on how to do this.
To make a Toast before drawing your layout resources, just do this following.
//put this code before your setContentView(R.layout.your_layout);
Toast.makeText(yourclassName.this,"your text here",5000).show();
Well, consider one thing. If you want this toast to be shown before loading your activity and notify users some message. Then it might not be possible always. Because, The time Toast is shown with fraction of nano/mili seconds your layout is being loaded as-well.Moreover, scenario is totally different when you are on a real device and on a emulator.This might be the cause you got a flash of your Toast message. Just run it on a real device and you will see the differences.
Hope that helps
I use Toast dialogs a lot throughout my application. However, I have noticed that after switching activities, the dialog will continue to stay visible until its timer has run out.
Toast.makeText( getApplicationContext(), R.string.toast_need_bt, Toast.LENGTH_LONG ).show();
I use Toast.LENGTH_LONG because the message is long and if the user decides to read it, the longer time option is needed. However once the user has used the application once or twice they will not need to read the toast messages and they will quickly move from activity to activity. However, these toast dialogs stay on the screen even while switching from activity to activity.
Is there a way to end all Toast Dialogs if the current Activity is terminated?
Call cancel() on the toast object when finishing/leaving the activity
Here is a link to the documentation Toast
Toast.makeText returns a Toast object. Call cancel() on this object to cancel it.
Check this post for more answers on this topic How to cancel Toast
Even though there are a few answers already on how to use the .cancel() method, i would like to add a few options to this usecase:
1) Create in layout notifications Cyril Mottier's Article here
2) Display the toast only the first X times
3) Create a dialog with a "Show notifications" checkbox to allow the user to opt out.
I have run this method
sToast.cancel();
can I call this method
sToast.show();
I have tried it.the sToast can show in GB, but not in ICS.
Is it right?
As per the documentation, calling show() after cancel() doesn't show any Toast on screen. Do check properly.
Yes, you can use Toast.cancel(). Usually a Toast cancels by itself after a timeout so usually programmers never end up using cancel. If there is a need, you can very well go ahead and use it.
I was reviewing a student's program, which had code like this within an Activity:
Toast toast = Toast.makeText(this, "Hello", Toast.LENGTH_LONG);
toast.show();
toast.setText("Goodbye");
This displayed the text "Goodbye", which was initially quite a surprise. I assume this happened because the call to show() merely queues up a request to display the Toast instance and returns before it is actually displayed. The call to setText("Goodbye") mutates the instance before it is displayed.
Two questions:
Is my interpretation correct?
What's the best way to ensure serial semantics in the presence of Toast mutation?
When in doubt it's best to consult the source.
Toast internally uses a static reference to INotificationManager and calls it's enqueueToast method every time a Toast.show() is called.
It synchronizes around a List of Toasts so that only one Toast at a time is showed - this is needed if multiple Toast.show() are called then shows them one after another with their set duration.
Since Toasts references are enqueued (actually Toasts internal class TN is), calling setText() changes the enqueued Toast.