How can I cancel a toast message that I defined in a previous activity from another intent?

StackOverflow https://stackoverflow.com/questions/22488611

  •  16-06-2023
  •  | 
  •  

Вопрос

I'm having trouble closing my toast message that I'm creating in the activity BarcodeActivity after I pass over the intent to another class, UPCHandler. Since I can't pass toasts through intents, I haven't been able to find a way to manipulate the toast message from the UPCHandler intent.

BarcodeActivity:

public void didScanBarcode(String barcode, String symbology) 
{
    // Remove non-relevant characters that might be displayed as rectangles
    // on some devices. Be aware that you normally do not need to do this.
    // Only special GS1 code formats contain such characters.
    String cleanedBarcode = "";

    for (int i = 0 ; i < barcode.length(); i++) 
    {
        if (barcode.charAt(i) > 30) 
        {
            cleanedBarcode += barcode.charAt(i);
        }
    }

    try
    {
        //stops scanner
        mBarcodePicker.stopScanning();
        Log.e("CleanedUPC", cleanedBarcode);

        /* Toast Setup */
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                                       (ViewGroup) findViewById(R.id.toast_layout_root));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText("Fetching your results...");

        Toast toast = BarcodeActivity.toast;
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout); 
        toast.show();

            @Override
            public String dataRetreived() 
            {/*
                Intent i = new Intent(getApplicationContext(), UPCHandler.class);

                Bundle bundle = new Bundle();
                bundle.put("value", inputTransacVal.getText());

                i.putExtras(bundle);

                startActivity(i);*/
                ItemsDto idt = hand.getFirstUpc();

                if (idt == null) 
                {
                    Log.e("BarcodeActivity:didScanBarcode", "UPC is NULL");
                    toast.cancel();
                } 
                else 
                {
                    Intent intn_display = new Intent("com.ahold.scan.DISPLAYSCREEN");
                    Intent intn_warning = new Intent("com.ahold.scan.WARNINGSCREEN");

                    ImageLoader imgLoad = new ImageLoader(idt.getImageUrl(), context, idt)


                    */

                }
                return null;

            }
        });

    }
    catch(Exception e)
    {
        //throws an error message to logcat if it catches one
        Log.e("IntentStartError", e.toString());
    }
}
Это было полезно?

Решение

There are two things you can try to do:

  1. Close the toast before switching activities, pass an indication in your intent that the toast needs to be reshown, and show it in the new activity.
  2. Pass the toast. Put it in a static variable somewhere, and in your new activity cancel it if it's not null. Keep in mind that it might be null, because Android can kill your process and start it again between activities.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top