13
votes

I am using the Action Bar in my Android application, and i am using sliding animation during activity transition, by calling:

startActivityForResult(i, ALL_OK);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

where in my xml files i have the following code:

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
 <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="400"/>  
</set>

and

<set xmlns:android="http://schemas.android.com/apk/res/android">   
 <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="400"/>
</set>

The animation is executed successfully, but the slide also include the Action Bar.

Is there a way that the Action Bar will stay stable during the activity transition ?

2

2 Answers

13
votes

Is there a way that the Action Bar will stay stable during the activity transition ?

No, sorry. If you are switching between activities, the entire UI changes, action bar and all.

If your desired UI behavior is essential, rewrite your app to have one activity and multiple fragments, and use animations for transitioning between the fragments.

4
votes

It can be done in Android Lollipop by using Activity Transition Animations, using the same layout for the action bar in all activities and defining the action bar to be a Shared Element in the following way:

First, make sure to enable activity transitions in v21/styles.xml and use NoActionBar theme so that you can create a custom action bar:

<style name="AppTheme.NoActionBar">
    ...
    <!-- enable window content transitions -->
    <item name="android:windowActivityTransitions">true</item>
    <!-- specify enter and exit transitions -->
    <item name="android:windowEnterTransition">@android:transition/slide_right</item>
    <item name="android:windowExitTransition">@android:transition/slide_left</item>
    <!-- specify shared element transitions -->
    <item name="android:windowSharedElementEnterTransition">@transition/app_bar_transform</item>
    <item name="android:windowSharedElementExitTransition">@transition/app_bar_transform</item>
</style>

Then define your app_bar_transform.xml (in res/transition directory) to be changeBounds like this:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
    <changeBounds/>
</transitionSet>

Since the app bar layout is the same in all the activities, no transformation will occur.

In your custom AppBarLayout set the android:transitionName attribute:

<android.support.design.widget.AppBarLayout 
    ...
    android:id="@+id/app_bar"
    android:transitionName="appBar"
    ...
</android.support.design.widget.AppBarLayout>

Use this layout for the action bar in all of your activities:

<include layout="@layout/app_bar"/>

Then, when you want to start the next activity define the app bar layout as a shared element like this:

final Intent intent = new Intent(getApplicationContext(), MainActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, toolbar, "appBar");
ActivityCompat.startActivity(WelcomeActivity.this, intent, options.toBundle());

This will keep the app bar in place when transitioning between activities.