Javier Sivianes
Javier Sivianes

Reputation: 792

How does Fragment Transaction work with animations?

I am having a really annoying situation with Fragment Transaction.

In my activity there is a fragment that must appears with a slide up animation:

public void showConnectedToWifiCard(String ssid) {
    Bundle bundle = new Bundle();
    bundle.putString("SSID", ssid);

    ConnectedToWifiCardFragmentImpl fm =  new ConnectedToWifiCardFragmentImpl();
    fm.setArguments(bundle);

    // Begin the transaction
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    // Replace the contents of the container with the new fragment
    //ft.setCustomAnimations(R.anim.slide_up_anim, R.anim.slide_down_anim);

    //ft.hide(fm);
    if (ft == null){
        ft.add(R.id.main_small_card, fm);
    }else{
        ft.replace(R.id.main_small_card, fm);
    }

    ft.commit();
}

I supposed that Fragment Transaction would create the fragment (or replace) right after the animation, but, the result is that:

I have tried to set invisible/visible the fragment but the result doesn't change.

How can I solve this "blink"?

Upvotes: 1

Views: 812

Answers (3)

Javier Sivianes
Javier Sivianes

Reputation: 792

I recoded the animations:

Slide_up.xml

<translate
    android:fromXDelta="0"
    android:fromYDelta="1000"
    android:duration="1000"/>

slide_down.xml

<translate
    android:fromXDelta="0"
    android:fromYDelta="-1000"
    android:duration="1500"/>

Upvotes: 0

Hong Thai
Hong Thai

Reputation: 218

For making animation when replacing Fragment, you can use this code:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

            //set customize animation here
            fragmentTransaction.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out,
                    R.anim.slide_right_in, R.anim.slide_right_out);

            ExampleFragment fragment = ExampleFragment.getInstance();

            fragmentTransaction.replace(R.id.fragment, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

Please see my blog post for more details: http://www.devexchanges.info/2015/05/replacing-fragment-and-adding-layouts.html Hope this help! :D

Upvotes: 2

Csbaek000
Csbaek000

Reputation: 31

I would rather attach view element to the fragment and use invalidate() than replace fragment everytime when the fragment is created. Or you could use animation library which is introduced in android DOC.

Upvotes: 0

Related Questions