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
Android: When Is Oncreateoptionsmenu Called During Activity Lifecycle
Best Way to Update Data with a Recyclerview Adapter
How to Populate Android Room Database Table on First Run
Emulator Appearing Offline on M1 MAC After the Last Update of Arm64-V8A
On Android 8.1 API 27 Notification Does Not Display
How to Make an Intent with Multiple Actions
How to Get the Size of a Folder on Sd Card in Android
Android Finish() Method Doesn't Clear App from Memory
Custom Format Edit Text Input Android to Accept Credit Card Number
Avoiding Content Type Issues When Downloading a File via Browser on Android
Recycler View Showing Single Item
How to Put the Text on the Left of a Radio Button in Android
Android Studio 3.1 Cannot Resolve Symbol (Themes, Widget, Attr, etc.)
How to Use Recyclerview.Scrolltoposition() to Move the Position to the Top of Current View
Why Ondraw Is Not Called After Invalidate()
The Application Icon Does Not Show on Action Bar
Android Httpclient Hangs on Second Request to the Server (Connection Timed Out)