1

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);
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

2 Answers2

0

Android System internally handles the backstack for the activities. You don't need to explicitly handle going back if you're not supposed to override the default behavior. In your case, On pressing back, you're not popping the current activity, instead you're pushing a new one into the backstack. This will result in ambiguous behavior of your back button.

Maddy Blacklisted
  • 1,190
  • 1
  • 7
  • 17
0

First, the onBackPressed function is not required to be implemented only if you are caring about taking care of the activity stack (i.e. finishing up your third activity will bring back the second and finishing the second activity will bring up the first one and so on). The activity stack is already taking care of it.

Secondly, if you want to pass the data to a caller activity from the activity that you have opened from the caller, this is not the ideal way to do so. I would like to recommend you to read this documentation where it explains how to pass such intents in a graceful manner using the life-cycle functions provided by Android.

Just to give you a heads up, here are three steps for you to pass the data between activities in a backward direction.

  1. You need to start the second activity from the first activity using startActivityForResult instead of startActivity with a request code passed along with it (so that you can differentiate between different request codes from other activity if there is any).
  2. Then in your second activity, if you need to pass something to your caller activity (i.e. the first activity), you need to use setResult to pass the desired data back to the caller and finish() the second activity.
  3. In the first activity, you need to implement onActivityResult function to get the data back from the second activity.

Here is a nice example of the overall implementation where you can take a look too.

I know that you are expecting that the system.exit(0) should kill the application whatever the situation is. However, in Android, it does not work in the way that you have expected I think. I am quoting from this answer on StackOverflow to explain how the system.exit(0) works.

System.exit(0) - Restarts the app with one fewer activity on the stack. So, if you called ActivityB from ActivityA, and System.exit(0) is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityA.

I hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • I am using custom on back pressed method because i need to create a new instance of the activity when the back button is pressed.Can you say why the back button when pressed at the main activity goes to the second activity??? – Marnish Prabhu Aug 20 '19 at 13:04
  • I think, I already have mentioned that in my answer. Please check the portion where I tried to explain how the `system.exit(0)` works. – Reaz Murshed Aug 20 '19 at 15:31
  • can you ex[plain me what is an activity stack?? – Marnish Prabhu Aug 21 '19 at 14:02
  • When you launch an `Activity` from another `Activity` the android framework keeps the track of the activities started in a stack so that when you `pop` from the stack, you get back the previous activity. To learn more, please see the [developer's documentation here](https://developer.android.com/guide/components/activities/tasks-and-back-stack). – Reaz Murshed Aug 21 '19 at 18:26