简体   繁体   中英

How to switch activity after catching an exception in android?

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

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

If you want to start a new activity,

Use Intent

Return to root/previous activity call onBackPressed();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM