Mehmed
Mehmed

Reputation: 3040

How to finish activity with custom animation when home button pressed

I have an ActionBarActivity "B" whose parent is ActionBarActivity "A" (also defined in manifest). A is in "singleTask" launch mode. I have an animation when starting B from A as follows:

public void onItemClick(...) {
    Intent mIntent = new Intent(getActivity(), B.class);
    startActivity(mIntent);
    getActivity().overridePendingTransition(R.anim.B_in, R.anim.A_out);
}

On B, I have the following onOptionsItemSelected and onBackPressed:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        getSupportFragmentManager().popBackStackImmediate();
        //onBackPressed();
        //finish();
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.A_in, R.anim.B_out);
}

Here is the problem: When I press back button, the animation at onBackPressed is taking place as expected. However, when I click on icon at top left in the actionbar, popBackStackImmediate is called and Android's default animation is played which is different. So:

  1. How can I manage to get same animation as in onBackPressed?
  2. Should I use onBackPressed() instead of popBackStackImmediate()? Will it give the same result as popBackStackImmediate do?

Any suggestions and best practices are welcome...

Upvotes: 0

Views: 4096

Answers (1)

Dave S
Dave S

Reputation: 3468

You could use .popBackStack() instead of popBackStackImmediate() then overrride the pendingTransition, that might work. Since these are both activities though, my inclination would be to call finish(); then overridePendingTransition().

Upvotes: 2

Related Questions