Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen flicker while fragment transition

Here is the link to recording while pressing back button

While i am using a animation in a fragment transaction it's working fine but i am getting a flicker of the next screen which is annoying me. I have been searching for it since 2 days no progress.

I am using this code for transition

public void moveToBaseSelect() {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left,R.anim.enter_from_left,R.anim.exit_to_right);
    ft.replace(R.id.home_frame, new BaseSelectFragment(), HomeActivity.BASE_SELECT);
    ft.addToBackStack(HomeActivity.BASE_SELECT);
    ft.commit();

}

public void moveToLogin()
{
    if(fragmentManager.getBackStackEntryCount()>=1 && fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1).getName().equals(HomeActivity.LOGIN))
        return;

    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left,R.anim.enter_from_left,R.anim.exit_to_right);
    ft.replace(R.id.home_frame, new LoginFragment(), HomeActivity.LOGIN);
    ft.addToBackStack(HomeActivity.LOGIN);
    ft.commit();
}

The anim files

enter from left

<set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromXDelta="-100%p"
        android:toXDelta="0%p" />
</set>
</set>

Enter from right

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

exit to left

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

exit to right

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

I have tried a alternative that's working by using animator instead of anim and app.fragment instead of v4.fragment

But I am extremely curious to know is there any solution if i stick with the anim method

like image 707
Milind Chaudhary Avatar asked Mar 01 '17 09:03

Milind Chaudhary


2 Answers

Remove your <set> elements in your animation files. I just had this problem and It disappeared when I removed them.

My xml for an animation:

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

like image 59
A.Danibo Avatar answered Nov 15 '22 03:11

A.Danibo


The issue is because commit() is asynchronous and presumably some race condition causes the flickering. Solved by making transaction manager do commitNow() instead of commit()

like image 22
pete Avatar answered Nov 15 '22 04:11

pete