Constant toast message: the toast will not disappear after execution

I have a message with a toast that does not disappear after execution. I suppose this has something to do with being in a loop, but I'm not sure. Can someone help me figure out why the toast message doesn't fall apart?

@Override public void onClick(View v) { int index = 0; for(int i= 0; i<userArray.length; i++){ if(email.getText().toString().equals(userArray[i])){ index = i; } if(passArray[index].equals(password.getText().toString())){ Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getBaseContext(), "INVALID LOGIN", Toast.LENGTH_SHORT).show(); } } Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class); startActivityForResult(mainIntent, 0); } 

}

+4
source share
9 answers

A Toast message may get stuck if you call it from a service flow, and this thread exits until Toast disappears. Then you get stuck in the Toast message on the screen until you kill the application.

+9
source

Toast always disappears. Always.

Therefore, you are displaying several Toast posts. Guaranteed. Check this by changing the messages using some kind of counter or turning it off after the initial is displayed.

Please note that quite a few Toasts can add up and take time to finish the show back. Wait, and they will eventually leave until your application loops indefinitely.

Edit: I fixed it. Apparently, Toast messages can get stuck and remain displayed if they start with Thread, Service, or IntentService. Thanks @Johnathan and @zapotec.

+3
source

A Toast does not always disappear.

"If you call it from a service thread and this thread exits before Toast disappears, you are stuck with a Toast message on the screen until you kill the application.

(copied from Jonathan Cole 's original answer, which unfortunately was deleted)


Here is an example of a stack trace that explains how a situation could happen:

  java.lang.IllegalStateException: Handler (android.os.Handler) {123f93ea} sending message to a Handler on a dead thread at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325) at android.os.Handler.enqueueMessage(Handler.java:631) at android.os.Handler.sendMessageAtTime(Handler.java:600) at android.os.Handler.sendMessageDelayed(Handler.java:570) at android.os.Handler.post(Handler.java:326) at android.widget.Toast$TN.hide(Toast.java:387) at android.widget.Toast.cancel(Toast.java:133) 
+2
source

I had this problem. I found that I created over 10 toasts. Each of them waited until the next one disappeared before displaying the next, with the total number of seconds on the screen.

I found that storing all the toasts in an arraylist, and then canceling them all fix my problem.

 private ArrayList<Toast> toasts; public void sendToast(final String message) { if (null == toasts) { toasts = new ArrayList<Toast>(); } runOnUiThread(new Runnable() { @Override public void run() { try { Toast toast = Toast.makeText(NJActivity.this, message, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 300); toast.show(); toasts.add(toast); } catch(Exception e) {/* do nothing, just means that the activity doesn't exist anymore*/} } }); } public void removeToast() { runOnUiThread(new Runnable() { @Override public void run() { if (null != toasts) { for(Toast toast:toasts) { toast.cancel(); } toasts = null; } } }); } 

Then when destroying at a stop, I called the removeToast () method

 @Override public void onDestroy() { super.onDestroy(); removeToast(); } @Override protected void onStop () { super.onStop(); removeToast(); } 
+1
source

There are strange cases when the toast does not disappear. To handle this, just use a handler. Create it as a field and use "post (Runnable)" with a toast image.

+1
source

Here is my solution with a custom toast handler. Works great in m application.

 public class CustomToast { private LayoutInflater inflater; private Toast toast; private View customToastLayout; private TextView LblCustomToast; public CustomToast() {} public void showCustomToast(String toastMessage, int toastLength, AppCompatActivity context) { if(inflater == null) {inflater = context.getLayoutInflater();} if(customToastLayout == null) {customToastLayout = inflater.inflate(R.layout.toast_layout, (ViewGroup) context.findViewById(R.id.toast_layout_root));} if(LblCustomToast == null) {LblCustomToast = (TextView) customToastLayout.findViewById(R.id.LblCustomToast);} LblCustomToast.setText(toastMessage); if(toast == null) {toast = new Toast(context);} toast.setGravity(Gravity.CENTER_VERTICAL| Gravity.BOTTOM, 0, DPDensity.getPxFromDP(160, context.getResources())); toast.setDuration(toastLength); toast.setView(customToastLayout); if(toast.getView().isShown()) {toast.cancel();}//Toast is cancelled here if currently showing toast.show(); } } 

Here is the XML layout

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="15dp" android:paddingTop="5dp" android:paddingRight="15dp" android:paddingBottom="5dp" android:orientation="vertical" android:background="@drawable/custom_toast_bg" android:gravity="bottom|center" android:layout_marginBottom="50dp"> <TextView android:id="@+id/LblCustomToast" android:text="Test" android:textSize="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/lightLime" android:layout_gravity="center" android:gravity="center"/> </LinearLayout> 

And here is the use:

 customToast = new CustomToast(); customToast.showCustomToast(getString(R.string.your_message), Toast.LENGTH_SHORT, (AppCompatActivity) mainActivityContext); 
+1
source
  I m guessing it has something to do with it being in the loop but I'm not sure. 

Yes, you are right because you saved it in a for loop, since many times the loop will execute so many times when Toast appears.

You show Toast several times in your code.

I would be better off if you didn't want him to crave many times, and then you can write it in a for loop.

-1
source

I think the problem is that the toast appears again and again on the screen, and its time is running out. Check your code by putting the outer toast cycle, and also check if the conditions are true several times. In short DEBUG your code.

-1
source

The toast stays a little longer. It will always fade generated. As in the cycle, they are continuously generated on top of each other, so it gives you the illusion of existence always. if you remain patient and wait a bit, all this will disappear after a while.

If you want to show a success message.

Try making an alert dialogue and keep updating the message on some Text View success message that you want to show on success and failure.

or try upgrading Toast only when it cannot log in.

Or check out the Infintie Loop if you want to take the same approach.

-1
source

All Articles