I have four activity for my application, Main Activity -> second Activity-> Third Activity -> Fourth Activity
. Each Activity contains the onBackPressed
method implemented. Every time when the onBackPressed
method called, it sends an intent to the previous activity. For Example, in Fourth Activity onBackPressed
method, it contains an intent to go to the previous Activity
, which is the Third Activity. This works fine upto the MainActivity
. When I press on the back button on the MainActivity
it must exit the application. However, it just starts the next activity, which is the second Activity. What causes this bug???
Here is some relevant code.
From the fourth activity:
@Override
public void onBackPressed() {
Intent intent = new Intent(this,third.class);
intent.putExtra("id",FinalID);
intent.putExtra("Image Item", categoryItem);
startActivity(intent);
}
From the third activity:
@Override
public void onBackPressed() {
Intent intent2 = new Intent(this, second.class);
intent2.putExtra("Image Item", categoryItem);
Toast.makeText(this, "theFinalActivity=" + categoryItem.getId(), Toast.LENGTH_SHORT).show();
startActivity(intent2);
}
The second activity looks like the following.
@Override
public void onBackPressed() {
next = false;
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
And finally the code in the MainActivity
:
@Override
public void onBackPressed() {
System.exit(0);
}