1

I have activity AActivity which calls Activity BActivity. In the AndroidManifest I specify B as follows:

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

If I'm in Activity B and click on the phone's Back-Button, then I go back to Activity A without calling A.onCreate(); again - that's how I want it to be. But If I'm in Activity B and click the ActionBar's Back button on the top left, then I go back to Activity A again, but A.onCreate(); is called. How can I use the backbutton of the top of the Activity to behave in the manner the phone's back button does, i.e. not calling StartingActivity.onCreate() ?

Btw: Activity A and B are extending from AppCompatActivity.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
MojioMS
  • 1,583
  • 4
  • 17
  • 42

4 Answers4

1

Inside your OnClick(View view) function call finish().
Related guide

Nir Duan
  • 6,164
  • 4
  • 24
  • 38
1

you can use android:launchMode="singleTop" in manifest inside activity A. read this following link for more details return parent activity correctly

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

Call finish() onBackPressed() and onOptionsItemSelected() when id == android.R.id.home

@Override
public void onBackPressed(){

    if(EcologDriverAIDApplication.DEBUG){

        Log.i(TAG, "onBackPressed");

    }
    finish();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //Back button pressed
    if (id == android.R.id.home) {
        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Miguel Benitez
  • 2,322
  • 10
  • 22
0

How can I use the backbutton of the top of the Activity to behave in the manner the phone's back button does, i.e. not calling StartingActivity.onCreate() ?

//declare android:onClick="backButtonPressed" in your xml in the back button layout
public void backButtonPressed(View view) {
    onBackPressed();
}

Why it should work:

When you press the bottom back button of android, onBackPressed() is called. As you have said, the bottom back button tap works the way you want it to. Therefore, just call it from your click listener of the back button at the action bar

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91