Oded Regev
Oded Regev

Reputation: 4405

How to quit the app from AlertDialog

I'm trying to do a very simple thing, show alert with only 1 button, if clicked I want the dialog to close and than the app to quit\finish()

currently I'm getting on the device the generic alert:

The application has stopped unexpectedly. Please try again

In the LogCat I get: Unable to pause activity

Here is the log:

01-03 14:49:00.670: ERROR/AndroidRuntime(22536): Uncaught handler: thread main exiting due to uncaught exception
01-03 14:49:00.680: ERROR/AndroidRuntime(22536): java.lang.RuntimeException: Unable to pause activity {com.SprintTwo/com.SprintTwo.SprintTwo}: java.lang.NullPointerException
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.access$2400(ActivityThread.java:119)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1874)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.os.Looper.loop(Looper.java:123)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at java.lang.reflect.Method.invoke(Method.java:521)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at dalvik.system.NativeStart.main(Native Method)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536): Caused by: java.lang.NullPointerException
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.phonegap.DroidGap.onPause(DroidGap.java:736)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at com.worklight.androidgap.WLDroidGap.onPause(WLDroidGap.java:163)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.Activity.performPause(Activity.java:3782)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)
01-03 14:49:00.680: ERROR/AndroidRuntime(22536):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3149)

Here is my code:

buttonClickListener = new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }

AlertDialog.Builder dlg = new AlertDialog.Builder(context);
    dlg.setTitle(title);
    dlg.setMessage(message);
    dlg.setCancelable(false);
    dlg.setPositiveButton(buttonText, buttonClickListener);
    dlg.create();
    dlg.show();

Upvotes: 0

Views: 7367

Answers (3)

sohilv
sohilv

Reputation: 1712

Issue is you cannot finish activity from Pause mode.

Idea is to handle it with flag. Write the Exit code in Resume() method with the flag check. Enable the flag in Dialog.

Something like (below code is dummy),

boolean APP_EXIT_FLAG = false;
Resume(){

if(APP_EXIT_FLAG)
finish();

}

And your alert dialog code,

buttonClickListener = new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                           APP_EXIT_FLAG = true;
                            dialog.dismiss();
                        }

AlertDialog.Builder dlg = new AlertDialog.Builder(context);
    dlg.setTitle(title);
    dlg.setMessage(message);
    dlg.setCancelable(false);
    dlg.setPositiveButton(buttonText, buttonClickListener);
    dlg.create();
    dlg.show();

Hope it helps.

Upvotes: 0

Sandroid
Sandroid

Reputation: 328

try if work

private void ConfirmAlert() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("CLOSE");
    builder.setMessage("Do You Want to Close the Application").setCancelable(false)
    .setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
            onYesClick();

        }


    }).setNegativeButton("No",
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
            onNoClick();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
private void onYesClick() {
    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);

    ActivityName.this.finish();



}private void onNoClick() {


}

Upvotes: -1

MikeIsrael
MikeIsrael

Reputation: 2889

try System.exit(0) instead of finish()

Upvotes: 6

Related Questions