You should add a flag:
private void exitQuiz() {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
سؤال
I have an activity from where on click of back button, the app should display home page, I have written a method for exiting the page as :
private void exitQuiz() {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
finish();
}
In the home page again when I am pressing back button, it's not exiting the application but stays on Home activity when I press back again then only it exits the application. I further tried by adding following code to home activity to handle such scenario:
public void onBackPressed() {
finish();
System.exit(0);
}
but still it still exiting on one click of back button. Any help will be highly appreciated.
المحلول
You should add a flag:
private void exitQuiz() {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
نصائح أخرى
You're probably creating a new instance of the HomeActivity when trying to get back to it.
Check it out for a solution: