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