Performing Action After Fragment Transaction Animation Is Finished

Event when the FragmentTransaction is completed

You don't need to wait for the fragment transaction to complete, You can call

getSupportFragmentManager().executePendingTransactions();

after your commit() function call.

This will ensure that the transaction is complete.

How to check finish setCustomAnimations when replace fragment in android

Instead of importing import android.app.Fragment;, use import android.support.v4.app.Fragment;. Change getFragmentManager in your replaceFragment method with getSupportFragmentManager. Then, in your fragment class, add the following:

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {

Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);

anim.setAnimationListener(new Animation.AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
// TODO: This is where you will put your code for when the animation is finished
}
});

return anim;
}

Put the code you want to execute after in onAnimationEnd.

FragmentTransaction animation to slide in over top

I found a solution which works for me. I ended up using a ViewPager with a FragmentStatePagerAdapter. The ViewPager provides the swiping behavior and the FragmentStatePagerAdapter swaps in the fragments. The final trick to achieve the effect of having one page visible "under" the incoming page is to use a PageTransformer. The PageTransformer overrides the ViewPager's default transition between pages. Here is an example PageTransformer that achieves the effect with translation and a small amount of scaling on the left-hand side page.

public class ScalePageTransformer implements PageTransformer {
private static final float SCALE_FACTOR = 0.95f;

private final ViewPager mViewPager;

public ScalePageTransformer(ViewPager viewPager) {
this.mViewPager = viewPager;
}

@SuppressLint("NewApi")
@Override
public void transformPage(View page, float position) {
if (position <= 0) {
// apply zoom effect and offset translation only for pages to
// the left
final float transformValue = Math.abs(Math.abs(position) - 1) * (1.0f - SCALE_FACTOR) + SCALE_FACTOR;
int pageWidth = mViewPager.getWidth();
final float translateValue = position * -pageWidth;
page.setScaleX(transformValue);
page.setScaleY(transformValue);
if (translateValue > -pageWidth) {
page.setTranslationX(translateValue);
} else {
page.setTranslationX(0);
}
}
}

}

Android Fragments and animation

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction.

Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

The following code shows how you would replace a fragment by sliding out one fragment and sliding the other one in it's place.

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);

DetailsFragment newFragment = DetailsFragment.newInstance();

ft.replace(R.id.details_fragment_container, newFragment, "detailFragment");

// Start the animated transition.
ft.commit();

To achieve the same thing with hiding or showing a fragment you'd simply call ft.show or ft.hide, passing in the Fragment you wish to show or hide respectively.

For reference, the XML animation definitions would use the objectAnimator tag. An example of slide_in_left might look something like this:

<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1280"
android:valueTo="0"
android:duration="500"/>
</set>

Fragment flashes after removal and animation ends

I think that the main problem is that commited FragmentTransaction executes asynchronously. If you want to immediately executing use getFragmentManager().executePendingTransactions().



Related Topics



Leave a reply



Submit