-1

I have a simple form in Android, and I want to show a message after clicking the Submit button and after some time it should go to another activity.

My code:

public void submitbuttonHandler(View view) {
    boolean isAllFieldsChecked = CheckAllFields();

    if (isAllFieldsChecked) {
        showMsgSnack("Success");

        Intent i = new Intent(AddOsivo.this, MainActivity.class);
        startActivity(i);
    }
}

    public void showMsgSnack(String msg) {
        View parentLayout = findViewById(android.R.id.content);
        Snackbar snackbar = Snackbar.make(parentLayout, msg, Snackbar.LENGTH_LONG);snackbar.getView().setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.container_snackbar));
        snackbar.show();
    }

The problem is, it is redirecting immediately and the message can't be seen.

I tried to use Snackbar.LENGTH_LONG, but it did not help.

I am thinking about using mHandler.postDelayed but not sure if there is any other solution.

So just to show a message for some seconds and only after that redirect to another activity.

Darksymphony
  • 2,155
  • 30
  • 54
  • `I tried to use Snackbar.LENGTH_LONG, but it did not help.` well, there's no reason why this would help, that just specifies how long to show the snackbar, that has nothing to do with the rest of your code, your code isn't going to wait for the snackbar to disappear before executing the rest, so depending on what you want to do, something like a handler to delay would help – a_local_nobody Mar 19 '22 at 11:28

1 Answers1

0

Ok, so for now I solved it this way as I don't know any other solution:

       mHandler.postDelayed(redirectForm, 2000);


private final Runnable redirectForm = new Runnable() {
    public void run() {
        Intent i = new Intent(MainActivity.this, OtherActivity.class);
        startActivity(i);
    }
};
Darksymphony
  • 2,155
  • 30
  • 54