HPEdag HPEdag - 4 months ago 11
Android Question

Animation between Activities, of which one is singleTask

I'm developing a chat app and am having trouble with figuring out how to get the transition between two activities to animate correctly. I have googled this for a while now and have also searched SO for it, but sorry in advance if this question has already been answered.

I have a

ChatMainActivity
, which holds a Fragment containing a list of chats and a
ChatContentFragmentActivity
, which holds a Fragment containing the chat messages. As seen below I have declared the ChatMainActivity as
launchMode="singleTask"
in my AndroidManifest.xml.

<activity
android:name=".ui.activities.ChatMainActivity"
android:launchMode="singleTask"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".ui.activities.ChatContentFragmentActivity"
android:parentActivityName=".ui.activities.ChatMainActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustResize" />


These are my animation .xmls:

chat_content_activity_enter_animation.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@integer/activity_animation_duration"
android:fromXDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0" />
</set>


chat_content_activity_exit_animation.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@integer/activity_animation_duration"
android:fromXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="100%" />
</set>


chat_list_activity_enter_animation.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@integer/activity_animation_duration"
android:fromXDelta="-100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="0" />
</set>


chat_list_activity_exit_animation.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@integer/activity_animation_duration"
android:fromXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="-100%" />
</set>


And here is how I use them:

In the
Adapter
of the
RecyclerView
containing the chats in
ChatMainActivity


Intent intent = new Intent(context, ChatContentFragmentActivity.class);
context.startActivity(intent);
activity.overridePendingTransition(R.anim.chat_content_activity_enter_animation, R.anim.chat_list_activity_exit_animation);


ChatContentFragmentActivity


@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.chat_content_activity_exit_animation, R.anim.chat_list_activity_enter_animation);
}


My Problem now is that the enter and exit animations of the
ChatContentFragmentActivity
are shown, but the
ChatMainActivity
does nothing.

There was a SO question that I can't seem to find now, that suggested, that singleTask was a problem here, but I remember the solution suggest in that question not helping me.

Does anyone know how I can get the exit and enter animation of my
ChatMainActivity
to play?

Edit: This is how it looks at the moment:Animation at the moment

Answer

use this .

pull_right.xml

 <?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="100%"
    android:toXDelta="0%"
    android:interpolator="@android:anim/decelerate_interpolator"
    />

push_left.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromXDelta="0%"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toXDelta="-100%" />

fade_out.xml

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

use in the Adapter this........

Intent intent = new Intent(context, ChatContentFragmentActivity.class);
context.startActivity(intent);
activity.overridePendingTransition(R.anim.pull_right, R.anim.push_left);

use this in the ChatContentFragmentActivity Class onBackPressed() method.......

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

and for keyboard just add these line in your xml file in EditText Tag of ChatContentFragmentActivity ....

android:focusable="true"
android:focusableInTouchMode="true"

enjoy coding ..........

Comments