Question

I'm trying to achieve the following.

I want to slide activity 2 in with a click on btn1 and want to slide activity2 out again with a click on btn2 as shown in the following image:

slide in/out

I try to do so using the following (c#) code; java equivelant should be somewhat similar:

    //MainActivity      
    btn1.Click += (object sender, EventArgs e) => {
        var stationActivity = new Intent(_mainActivity, typeof(StationInfoActivity));
        StartActivity(stationActivity);
        _mainActivity.OverridePendingTransition(Resource.Animation.slide_1_enter, Resource.Animation.slide_1_exit);
        //I have a reference to the _mainActivity since this activity is technically a fragment
    };

    //StationActivity
    btn2.Click += (object sender, EventArgs e) => {
        var mainIntent = new Intent(this, typeof(MainActivity));
        StartActivity(mainIntent);
        this.OverridePendingTransition(Resource.Animation.slide_2_enter, Resource.Animation.slide_2_exit);
    };

And the following resource files:

    //slide_1_enter
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromYDelta="100%p" android:toYDelta="0%p"
            android:duration="@android:integer/config_shortAnimTime"/>

    //slide_1_exit
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromYDelta="0%p" android:toYDelta="0%p"
            android:duration="@android:integer/config_longAnimTime" />

    //slide_2_enter
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromYDelta="0%p" android:toYDelta="0%p"
            android:duration="@android:integer/config_longAnimTime" />

    //slide_2_exit
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromYDelta="0%p" android:toYDelta="100%p" 
            android:duration="@android:integer/config_longAnimTime" />

The sliding in (slide_1_enter + slide_1_exit) is working perfectly, however, I can't figure out how to create the slide out animation (slide_2_enter + slide_2_exit). I don't visually see the exit animation. This is probably because of the following:

  • MainActivity is visible (z-index: 0).
  • StationActivity is launched with btn1.Click and animates over activity 1, using slide_1_enter.
  • StationActivity is visible (z-index: 1)
  • When clicking on btn2, MainActivity will launch, but it will be placed ON TOP OF StationActivity (getting z-index: 3), and thus preventing to see the exit animation (slide_2_exit)

So, unless I'm able to change the "z-index" of activities, this will not work, right?.

Is there a solution? Or any other suggestion? I'm afraid that the "easiest" way to get this working is to use a viewpager, but I rather would not want to implement this, because activity1 is already within a viewpager, and things would get way more complicated.

Was it helpful?

Solution 2

Argh... Just found my solution, and it was super easy. Well, guess I'm still in the learning phase, hopefully this question will help others though...

I was right in my assumption that the activities are stacked over each other, thus preventing to see the exit animation.

Simply adding the property "android:zAdjustment="top" in "slide_2_exit" fixed this for me...

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromYDelta="0%p"
    android:toYDelta="100%p" 
    android:zAdjustment="top"
/>    

OTHER TIPS

When overriding the transitions for button 1, call your 'slidein' animation. When you press button 2, call your 'slideout' animation which is the reverse of slidein. i.e slide in would have:

fromYDelta="-100%"
toYDelta="0%"

And slide out would have:

fromYDelta="0%"
toYDelta="-100%"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top