简体   繁体   中英

pressing back button two times closes activity

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

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.

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