I have a simple game that contains three pages: start_page
, play_page
and lose_page
. In the lose_page
I added home_button
that will take you to the start_page
to play again.
What I want is when I press the back button of the phone in the start_page
to take me out of the game, but the problem is the back button takes me to the previous page which is the lose_page
.
Here is the code of the home_button
in the lose_page
that takes you to the start_page
:
Button home_btn = (Button)findViewById(R.id.btnPlayAgain);
home_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(LostActivity.this,
HomeActivity.class);
startActivity(i);
}
});
Any help will be appreciated.
Try adding the finish() method to your code,
Button home_btn = (Button)findViewById(R.id.btnPlayAgain);
home_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(LostActivity.this,
HomeActivity.class);
startActivity(i);
//call the finish method
finish();
}
});
When calling finish() on an activity, the method onDestroy() is executed and will destroy your LostActivity which would prevent going back to LostActivity from HomeActivity.