Need to click back arrow twice to go back to previous activity, how to prevent it

33 Views Asked by At

I have one activity A and when I click on one button from activity A and come to an another activity B and when I click on back arrow from activity B to go back to A, its comes to the same activity B, so I need to click the back arrow again to go back and when I click on second time, it works.

I have used below approach.

@Override
public void onBackPressed() {
    super.onBackPressed();
}

I have also used onKeyDown() approach. But it does not work, its comes to the same activity B, so I need to click the back arrow again to go back to previous activity.

Is there any way to prevent this from happening?

1

There are 1 best solutions below

0
David Weber On

You need to define the parent Activity of your Activity in your Manifest and get lost of the onBackPressed method:

<activity
    android:name=".BActivity"
    android:parentActivityName=".AActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".AActivity"/>
</activity>

That is how it is done.