Search code examples
androidtoast

android toast not showing (though another does)


I am having a problem where a toast is not showing up. However the first and third toasts are shown and the second uses exactly the same syntax. The context should be the same everytime.

My app doesn't crash and I tried runOnUiThread() already to no avail.

I have been searching for an answer for quite some time now but can't seem to find the reason behind the problem.

Thanks in advance for the help



This is not the whole class but this is where it happens

public class SendActivity extends AppCompatActivity {
    private static final int PICK_FILE = 0;
    private static final int REQUEST_PERMISSION_SMS_SEND = 1;
    private static final int REQUEST_PERMISSION_SMS_READ = 2;
    public static final int MESSAGE_CAPACITY = 114; //amount of bytes that can be transmitted per message //TODO check value
    public static final long MAX_FILE_SIZE = 400_000; //TODO replace with exact value (bytes)

    private Long fileSize;
    private String fileName;
    private android.net.Uri uri;
    private boolean inputOK;
    private boolean receivedAuthorization;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
    }

    /**
     * Method called when the confirmation button is pressed. It will go to the transmitActivity
     * if it passes the verification
     *
     * @param view the view that called the method
     */
    public void transmitActivity(View view) {
        TextView tv = findViewById(R.id.numberInputInner);
        String number = tv.getText().toString();
        if (!inputOK || number.isEmpty() || !PhoneNumberUtils.isWellFormedSmsAddress(number)) { //only check if number is present and numerical
            // 1 SHOWN
            Toast.makeText(this, "Please enter a phone number and choose a correct file", Toast.LENGTH_LONG).show();
            return;
        }

        //check if allowed to send sms, else request
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_PERMISSION_SMS_SEND);
            return;
        }

        //check if allowed to read sms, else request
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, REQUEST_PERMISSION_SMS_READ);
            return;
        }

        try {
            receivedConfirmation = false;
            sendRequestMessage(); //sends an sms
            listenForResponse(); //creates a thread listening for response
            // 2 NOT SHOWN
            Toast.makeText(getApplicationContext(), "Waiting 15 sec for authorization from other party", Toast.LENGTH_LONG).show();
            Thread.sleep(15_000);
        } catch (InterruptedException e) {
            //TODO
        }

        if (!receivedConfirmation) {
            // 3 SHOWN
            Toast.makeText(this, "Didn't receive authorization to transfer the file. Try again", Toast.LENGTH_LONG).show();
            return;
        } else {
            Intent transmitActivity = new Intent(this, TransmitActivity.class);
            transmitActivity.putExtra("fileName", this.fileName);
            transmitActivity.putExtra("fileSize", this.fileSize);
            transmitActivity.putExtra("uri", this.uri);
            transmitActivity.putExtra("phoneNumber", tv.getText().toString());
            startActivity(transmitActivity);
        }
    }
}

Solution

  • Your code is running on the UI thread and Thread.sleep() blocks it. The UI thread does not get to process anything else, such as displaying toasts on the screen.

    Never use Thread.sleep() on an UI thread.

    You need some other mechanism for dealing with asynchronous operations such as waiting for something to complete. Callback methods are one usual way to do that.