Julius Lamar
Julius Lamar

Reputation: 33

Problems with fragment transitions

I have two fragments in an activity and I want the first one to gracefully slide out of view through the left of the screen while the second enters through the right of the screen. I am using the Transitions API and this is what happens:

Initial Screen:

Initial screen

Transition occurs poorly:

Transition occurs poorly

My code is given below:

HomeFragment savedHomeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag(HOME_FRAGMENT);

    if (savedHomeFragment == null) {
        mHomeFragment = new HomeFragment();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mHomeFragment.setEnterTransition(new Fade(Fade.MODE_IN));
            mHomeFragment.setExitTransition(new Slide(Gravity.START));
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.placeholder, mHomeFragment, HOME_FRAGMENT);
        fragmentTransaction.commit();
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            savedHomeFragment.setEnterTransition(new Fade(Fade.MODE_IN));
            savedHomeFragment.setExitTransition(new Slide(Gravity.START));
        }
    }

void buttonClick() {
    TargetFragment targetFragment = new TargetFragment();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        targetFragment.setEnterTransition(new Slide(Gravity.END));
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.placeholder, targetFragment , TARGET_FRAGMENT);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

What am I doing wrong?

Upvotes: 3

Views: 264

Answers (1)

MrJM
MrJM

Reputation: 1214

The buttons on your first fragment are placed upon a container without a background. The white background you are seeing is the background of your Activity. Provide a background color on the root element in your Fragment1 layout and your issue will be fixed.

Upvotes: 1

Related Questions