Search code examples
androidandroid-fragmentsandroid-animationandroid-toolbarandroid-transitions

Unable to start Android activity with animation


I have three layout files. Shared Flight Item is used in Simple Flight Item and Detailed Flight Item.

On clicking the simple flight item in a fragment hosted in activity A, I'm starting activity B that hosts a viewpager that holds the detail fragment. I'm unable to achieve the animation and more to that, the screen freezes and I have to click back twice to make the screen respond. So I feel the new activity is called, old screen is drawn on top of the new screen but the transition is not happening and hence the old screen is only visible. Is my conclusion right?

EDIT: I checked that the new Activity is getting created. The animation from old screen(which is drawn as an overlay over the new screen in the new activity) is not hiding to reveal the actual new screen.

Is the issue because of the shared item is in the CollapsingToolbar?

How to make it work?

Starting the new Activity:

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent intent=new Intent(getActivity(), FlightDetailActivity.class);
        intent.putExtras(args);
        ActivityOptions options=ActivityOptions.makeCustomAnimation(getActivity(),R.id.shared,R.string.transition_flight);
        ActivityCompat.startActivity(getActivity(),intent,options.toBundle());
    }

Shared Flight Item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@android:color/transparent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:transitionName="@string/transition_flight">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="right">
        <TextView
            android:id="@+id/from_code"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:textSize="22sp"
            android:textColor="@color/textPrimary"/>
        <!-- Other TextViews -->
    </LinearLayout>

</LinearLayout>

Simple Flight Item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@android:color/white"
    android:orientation="vertical">

    <include
        android:id="@+id/shared"
        layout="@layout/shared_flight_item" />
</LinearLayout>

Detailed Flight View:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingtoolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_gravity="center"
                android:gravity="center"
                app:layout_collapseMode="parallax">
                <include
                    android:id="@+id/firstFlight"
                    layout="@layout/shared_flight_item"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f3f3f3"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- <android.support.v7.widget.CardView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             card_view:cardElevation="2dp"
             card_view:cardUseCompatPadding="true"
             >-->
        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/primary"
                android:gravity="center">
                <include
                    android:id="@+id/secondFlight"
                    layout="@layout/shared_flight_item"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_margin="10dp">

                <include
                    android:id="@+id/operator"
                    layout="@layout/custom_detail_item_card" />

                <include
                    android:id="@+id/flightNumber"
                    layout="@layout/custom_detail_item_card" />

                <include
                    android:id="@+id/pnr"
                    layout="@layout/custom_detail_item_card" />

                <include
                    android:id="@+id/duration"
                    layout="@layout/custom_detail_item_card" />

                <include
                    android:id="@+id/cancellation"
                    layout="@layout/custom_detail_item_card" />

                <include
                    android:id="@+id/inclusion"
                    layout="@layout/custom_detail_item_card" />

                <include
                    android:id="@+id/details"
                    layout="@layout/custom_detail_item_card" />
            </LinearLayout>
        </LinearLayout>
        <!--</android.support.v7.widget.CardView>-->
    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

Solution

  • Instead of using the existing animations, I used make custom animation which caused the issue.