Android activity go back to activity that started it instead of parent activity when pressing navigation bar back button

Radian picture Radian · Oct 21, 2013 · Viewed 12k times · Source

I have the following scenario:

In android manifest I have three activities: ActivityA ActivityB - parent of ActivityA ActivityC

What I want to do is start ActivityA from ActivityC using intent.StartActivity(). The activity is started successfully. Now I want to go back to ActivityC using actionbar's back button (upper left corner), but since ActivityA has ActivityB as parent (as declared in android manifest) the actionbar back button takes me to ActivityB instead of previous ActivityC. If I use the back keyboard button, I get redirected to the ActivityC.

What can I do to get the same result in both "navigate back" cases. The result I'm seeking is to get redirected to the activity that started the ActivityA and not it's parent activity. Is it possible?

Answer

Juozas Kontvainis picture Juozas Kontvainis · Oct 21, 2013

You should not define ActivityB as parent for ActivityA in manifest. Instead, handle onOptionsItemSelected in ActivityA like this:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
        }
    return super.onOptionsItemSelected(item);
    }