0

I have one strange problem. I have an app with two activity "login" and "normalDifficulty". When I am in login activity and I press back button it closes activity gracefully. But When I am in "normalDifficulty" and I press back button it restarts my "normalDifficulty". To close "normalDifficulty" activity I have to press back button twice. No matter what i do it always closes "normalDifficulty" after pressing twice. Please help me why it is happening and how to fix this. The below code i used to close the activity.

 @Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("Are you sure you want to exit?")
        .setNegativeButton(android.R.string.no, null)
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()     {

            public void onClick(DialogInterface arg0, int arg1) {
                finish();
            }
        }).create().show();
}

I modified this by super.finish(), this.finish(), NormalDifficulty.finish(). But my activity always closes after pressing twice. Thank you.

Code where I am starting NormalDifficulty activity. this is happening in login activity where I decalared NormalDifficulty protected.

 enter.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            boolean userNameStatus, emailStatus;
            userNameStatus = validate(userName.getText().toString(), USERNAME_PATTERN);
            emailStatus = validate(email.getText().toString(), EMAIL_PATTERN);
            if(userNameStatus && emailStatus) {
                if(rememberMe.isChecked()) {
                    SharedPreferences settings = getSharedPreferences(
                            PREFRENCES_NAME, Context.MODE_PRIVATE);
                    settings.edit().putString("name", userName.getText().toString())
                            .putString("pwd", email.getText().toString()).commit();
                } else {
                    SharedPreferences settings = getSharedPreferences(
                            PREFRENCES_NAME, Context.MODE_PRIVATE);
                    settings.edit().clear().commit();
                }
            } else {
                showDialog("Invalid UserName or Email-ID");
                return true;
            }

            message = userName.getText().toString();
            startActivity(normalDiffculty);
            finish();
            return true;
        }
    });
user3687223
  • 185
  • 5
  • 14

1 Answers1

0

Check this.

You can add setCancelable(false) like this.

new AlertDialog.Builder(this)
    .setTitle("Are you sure you want to exit?")
    .setCancelable(false).
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()     {

        public void onClick(DialogInterface arg0, int arg1) {
            finish();
        }
    }).create().show();

This will prevent closing of dialog on clicking back after dialog is shown, and you can call activity's finish() in onClick of YES. And normal continuation in onClick of NO.

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • No help. Since it restarts the app so dialog closes. I did some work around, I override onRestart() method and called finsish() inside it so when app restarts after pressing back button it closes the app. But doing so it generates exception and then closes the app. Thanks – user3687223 May 29 '14 at 11:33
  • I replace finsish() from system.exit(0) even though it does not exit by pressing back button one time. Still i have to press back button twice to close the app. – user3687223 May 29 '14 at 12:14
  • Tried with "android.os.Process.killProcess(android.os.Process.myPid());" but even though my app restarts with different PID value. Still i have to press back button twice to close it. I am running app in emulator, could be because of that? – user3687223 May 29 '14 at 12:40
  • May be @user3687223 ry with device. – MysticMagicϡ May 29 '14 at 12:44
  • NO Still it gives same behaviour in emulator. I gave order for new andorid mobile.I will update you once my application will successfully execute in device. – user3687223 Jun 13 '14 at 17:19
  • I tried on real device but still it gives the same behavior. I have to press minimum two times back key to close the activity. Sometimes more than two times i need to press to close the activity. Any help? – user3687223 Jun 14 '14 at 09:54
  • Are you dealing with 1 activity or more than 1 activities in your applications? if more than 1, please finish prev activity while starting new one. @User3687223. I m having off today, so will try to help you on monday. :-) – MysticMagicϡ Jun 14 '14 at 10:45
  • Thank for the reply, I have three activity and I am closing every activity before going to next activity. I don't know how to fix this bug. will wait for your reply Thanks. – user3687223 Jun 14 '14 at 11:45
  • After long research finally I got the fix. The problem was that I was starting the activity inside Webview's ontouch listener. I don't know why but ontouch listener was firing randomly(twice or more than twice) when user touch the webview. So I declared one variable and did some work around so that my activity call only once. This problem was weird. – user3687223 Jun 15 '14 at 16:26
  • Oh great its fixed. Congrats :-) @user3687223 – MysticMagicϡ Jun 15 '14 at 18:27