2

Why is the toast message shown in this GIF disappearing before it should? I have tried displaying the toast message from the first activity (the password reset activity) right before starting the new activity (the login activity). I have also tried showing the toast message from the new activity in onResume() and it has the same effect. Also, as seen in the GIF, the toast message will reappear until it actually finishes if I tap on where it should be. EDIT: Actually, the toast message will reappear if I tap anywhere on the screen.

EDIT: Here is the code for the toast message:

auth.sendPasswordResetEmail(email)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(ResetPasswordActivity.this, LoginActivity.class));
                                finish();
                            } else {
                                Toast.makeText(ResetPasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
                            }

                            progressBar.setVisibility(View.GONE);
                        }
                    });

I get the same problem when I use Toast.LENGTH_LONG. It disappears in the same amount of time as Toast.LENGTH_SHORT, but it lasts longer (as expected) after I tap on it again to bring it back up like I do in the GIF. The thing is, I shouldn't have to tap to make it show for the full duration.

Vincent Nagel
  • 63
  • 1
  • 9
  • Post some code for the toast? – Danny Buonocore Dec 18 '18 at 22:15
  • Possible duplicate of [Can an Android Toast be longer than Toast.LENGTH\_LONG?](https://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long) – Martin Zeitler Dec 18 '18 at 22:49
  • This is not a duplicate question as the one asking how to make the Toast message last longer than Toast.LENGTH_LONG. I only need to show my message for Toast.LENGTH_SHORT. My problem causes both Toast.LENGTH_SHORT and Toast.LENGTH_LONG to disappear too soon. – Vincent Nagel Dec 20 '18 at 00:03

6 Answers6

0

You can easily change the length of the Toast by changing third argument:

Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();

Or

int duration;
Toast.makeText(this, "Hello World", duration).show();
Oktawian
  • 334
  • 1
  • 2
  • 16
  • I get the same problem when I use Toast.LENGTH_LONG. It disappears in the same amount of time as Toast.LENGTH_SHORT, but it lasts longer (as expected) after I tap on it again to bring it back up like I do in the GIF. The thing is, I shouldn't have to tap on to make it show for the full duration. – Vincent Nagel Dec 18 '18 at 22:40
  • That is what I am currently doing, "Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();" ResetPasswordActivity is the first activity. – Vincent Nagel Dec 19 '18 at 23:56
0

You can use handler to delay your function

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //do something 
        }
    }, 3000 ); //time in milisecond

edited as your code

 auth.sendPasswordResetEmail(email)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_LONG).show();
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        startActivity(new Intent(ResetPasswordActivity.this, LoginActivity.class));
                                        finish();
                                    }
                                }, 3000 ); 

                            } else {
                                Toast.makeText(ResetPasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
                            }

                            progressBar.setVisibility(View.GONE);
                        }
                    });
R4inV
  • 1
  • 1
  • Thanks for the tip, but I wanted to have the Toast message show up in the login activity after switching to it from the reset password activity. – Vincent Nagel Dec 19 '18 at 23:52
  • @VincentNagel try to edit your toast to " Toast.makeText(getApplicationContext(), "We have sent you instructions to reset your password!", Toast.LENGTH_LONG) " – R4inV Dec 20 '18 at 11:12
  • Thanks, but that didn't end up fixing it. I decided to just use snackbar instead. – Vincent Nagel Dec 21 '18 at 21:04
0

Just put Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();

And if it's not worked then uninstall app from device and install it

  • I put what you said in onResume of the login activity, and it is still doing the same thing. Reinstalling the app didn't help. I realize that it might be the correct behavior if I show it in one activity and switch to another because the documentation says "It only fills the amount of space required for the message and the current activity remains visible and interactive." However, I wouldn't think it should disappear early when called in onResume because that is when the user can be interactive with the activity. – Vincent Nagel Dec 19 '18 at 23:50
0

Try putting the Toast code after the finish() like:

auth.sendPasswordResetEmail(email)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                startActivity(new Intent(ResetPasswordActivity.this, LoginActivity.class));
                                finish();
                                Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(ResetPasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
                            }

                            progressBar.setVisibility(View.GONE);
                        }
                    });
Gab Ledesma
  • 1,785
  • 1
  • 7
  • 13
0

You can try start RestartPasswordActivity for result:

Intent intent = new Intent(this, Activity.class);
startActivityForResult(intent);

Destroy this activity by:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent); 
finish();

And get this value in LoginActivity this way:

@Override protected void onActivityResult(int requestCode, 
int resultCode, Intent data) {
    if (requestCode == 1) { 
        if(resultCode == Activity.RESULT_OK){
            boolean result = data.getBooleanExtra("result"); 
            if(result)
                Toast.makeText(this, "Text", Toast.LENGTH_LONG).show();
        }
    }
}
Oktawian
  • 334
  • 1
  • 2
  • 16
-1
Toast.makeText(MainActivity.this, "Hello Word!", Toast.LENGTH_LONG).show();
Paul Roub
  • 36,322
  • 27
  • 84
  • 93