How to resolve unable to pause activity in the given android code snippet?

Following is the android code snippet

runOnUiThread(new Runnable() {
                                public void run() {
                                    AlertDialog alertDialog;
                                    alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                                    alertDialog.setTitle("Network error");
                                    alertDialog.setMessage("Disconnected");
                                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
                                        /** ON clicking oKay button **/
                                        public void onClick(DialogInterface dialog, int id)
                                        {

                                            hasConnectServiceStarted = false;
                                            IP = null;
//                                            Intent intent = getIntent();
                                            Intent intentSplash = new Intent(getApplication().getApplicationContext(), SplashActivity.class);

                                            /** Shutdown the nodemain executor service if
                                             * HOST goes down
                                             */
                                            nodeMainExecutorService.forceShutdown();
                                            /** and restart the Main Activity that will prompt
                                             * user to reconnect to the HOST once again
                                             */

//  I want current activity to destroy before starting Splash Activity, hence calling onDestroy()
                                            onDestroy();
                                            startActivity(intentSplash);

                                        }
                                    });
                                    alertDialog.show();

});

Everything seems fine but when this alert dialogue comes and click OK, I get following error:

java.lang.RuntimeException: Unable to pause activity {edu.arizona.ece573.catgraph/edu.arizona.ece573.catgraph.MainActivity}: java.lang.IllegalStateException: No host at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3293) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3252) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3227) at android.app.ActivityThread.access$1100(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5292) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) Caused by: java.lang.IllegalStateException: No host at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1235) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) at android.support.v4.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:2056) at android.support.v4.app.FragmentController.dispatchPause(FragmentController.java:198) at android.support.v4.app.FragmentActivity.onPause(FragmentActivity.java:401) at android.app.Activity.performPause(Activity.java:6101) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1310) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3279) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3252)  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3227)  at android.app.ActivityThread.access$1100(ActivityThread.java:154)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5292)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

How should I resolve this error?

Upvotes: 1

Views: 15755

Answers (2)

Alexandru Ungureanu
Alexandru Ungureanu

Reputation: 151

How should I resolve this error?

Replace the call to

onDestroy()

with

finish()

You should also check the documentation to see which method does what.

Upvotes: 5

Kristiyan Varbanov
Kristiyan Varbanov

Reputation: 2509

Just call finish() after startActivity(intentSplash); and remove destroy() and you will achive that current activity will destroy.

Upvotes: 1

Related Questions