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.
Related
After user logout I want to cancel all API calls that are executing right now. So I found a way to do so
httpClient.dispatcher().cancelAll();
However in the callback side I need to know that this request was canceled to not retry it and not to show error message to user. But call.isCanceled() will be true if only call.cancel() was been called.
Dispatcher.cancelAll() just calls Call.cancel() for you.
I managed to fix this problem with moving to jxJava
I save all my Observable to CompositeSubscription
and after use compositeSubscription.unsubscribe(); to get rid of all callbacks
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.
Whenever I'm writing an Android app and making a Toast, I tend to call Toast.makeText() but then forget to call show() and get mad at myself later. Since there is no reason I would ever call Toast.makeText() and not mean to call show(), is there a way I can make Eclipse show a warning if I do that? Thanks
for clarity: I want Eclipse to show a warning anytime I call "Toast.makeText(blah, blah, blah)" instead of calling "Toast.makeText(blah, blah, blah).show()".
You mean like this?
Lint warnings can do lots of handy things - page through them and see what suits you!
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 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();