'Android - show message after form submit and go to another Activity

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.



Solution 1:[1]

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);
    }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darksymphony