0

This is my Activity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    Log.d("Activity:", "SHOW");
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.trans_right_in, R.anim.trans_right_out);
    Log.d("Activity:", "EXIT");
}

Whenever i enter this activity, "SHOW" appears in the log. But if i click the back button, the onBackPressed() doesn't get called and thus overridePendingTransition() neither.

I also set a breakpoint there and started the app in debug mode with the same result.

I'm using minSdkVersion 10 and targetSdkVersion 21.

What am i missing here?

2
  • which back button do you press? the hardware or the arrow on the actionbar? Commented Dec 7, 2015 at 16:57
  • the arrow on the actionbar. Ahhh when i Press the hardware back it works. But which method does get called when i press the arrow on the actionbar? Commented Dec 7, 2015 at 17:08

1 Answer 1

1

You have to make a method which makes the actions you want and then call the onBackPressed(). Then you can call the method on the onBackPressed() and on your arrow button

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            goBack();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void goBack(){
    super.onBackPressed();
    overridePendingTransition(R.anim.trans_right_in, R.anim.trans_right_out);
    Log.d("Activity:", "EXIT");
}

@Override
public void onBackPressed() {
    goBack();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.