Question

The code below has caught an exception. Now i want to switch to my MainActivity from the current activity just after the Toast message has been displayed. Is it possible ? if yes how?

catch(JSONException e){
     Toast.makeText(getBaseContext(), "Word is not availabe" ,Toast.LENGTH_LONG).show();
}
Was it helpful?

Solution

The default value used for Toast.LENGTH_LONG is 3500 ms. So, if you want to start your Activity after the Toast is shown, you could post a delayed Runnable for a slightly longer duration, like 4000 ms.

Here's an example:

    final Toast toast = Toast.makeText(getBaseContext(), "Word is not availabe",
            Toast.LENGTH_LONG);
    toast.show();
    toast.getView().postDelayed(new Runnable() {

        @Override
        public void run() {
            startActivity(new Intent(getBaseContext(), YourNewActivity.class));
        }
    }, 4000);

OTHER TIPS

If you want to start a new activity,

Use Intent

Return to root/previous activity call onBackPressed();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top