Change the z-order of activity animations on Andro

2020-07-18 07:37发布

问题:

Is there a way how to invert the z-order of activity animations? Basically when you start new activity it is added to the backstack and its window is added over the window of the previous activity. When returning back sometimes later the activity which which is taken from the backstack show its window underneath the top activity. Most time it makes completely sense, but I have a following issue with that behavior.

I have a widget which points somewhere deep into the app structure. So I have to manually create the back stack when the user clicks on the up button in action bar. But then I actually call startActivity with new intent pointing at a new activity with back stack attached to it..and here we go to the issue..The actvity actually animates from the top, instead of being animated as usual when poping from back stack the standard way.

you can see the flow on the following picture

here is a code snippet of how I am recreating the task back stack and navigating the user to the parent activity. also way below you can see a screenshot of the animation..Basically the window which is on top should be on bottom (it is higher in the app hiearchy). FYI all those animations are done via standard XML window animations and fired with overridePendingTransition calls. I tried to modify z-adjustment within those animations, but it seems like it only does affect various layers in one particular animation..

if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
       NavUtils.navigateUpTo(this, upIntent);
} else {
      TaskStackBuilder.create(this)
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();

}

回答1:

Old question, but I've had the same problem. The solution is to add the attribute android:zAdjustment="top" or android:zAdjustment="bottom" in our animation definitions. Example:

nothing_background.xml

<?xml version="1.0" encoding="utf-8"?>
  <translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:zAdjustment="bottom"
    android:duration="400"
    android:fromXDelta="0%p"
    android:toXDelta="0%p" >
  </translate>

fade_in_foreground.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:zAdjustment="top">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="400" />
</set>

In you code that triggers the transition:

Intent intent = new Intent(FromActivity.this, ToActivity.class);
Bundle animate = ActivityOptions.makeCustomAnimation(getApplicationContext(),
  R.anim.nothing_background, R.anim.fade_in_foreground).toBundle();
startActivity(intent, animate);