2

I have two fragments in an activity. When i go from fragment A to fragment B, custom animation have been added. So that the view of fragment B slides in from the right on entry and then slides back out from right on exit.

But the fragment B can been seen to be entering from behind the view of fragment A. So when Fragment A is replaced by Fragment B, the view of Fragment B animates from right to left but it comes in from behind the view of Fragment A and not in front of Fragment A.

I am able to notice this since half of Fragment A is transparent and Fragment B sliding in can be seen only from that transparent half indicating that it is sliding in from behind Fragment A and not from the front.

Not entirely sure why this is happening. The respective code has been provided below. Could someone please help?

    private void navigateToFragment(@NonNull Fragment fragment, boolean addToBackStack, int enterAnim, int exitAnim, int popEnterAnim, int popExitAnim) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            final FragmentTransaction transaction = fragmentManager.beginTransaction();


            transaction.setCustomAnimations(enterAnim, exitAnim, popEnterAnim, popExitAnim);
            transaction.replace(R.id.member_address_root, fragment, fragment.getClass().getSimpleName());
            if (addToBackStack){
                transaction.addToBackStack(fragment.getTag());
            }

            fragmentManager.executePendingTransactions();
            transaction.commit();
        }



 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_address);

        if (savedInstanceState == null){

            if (getIntent() != null){
                Fragment fragment = FragmentA.newInstance();
                navigateToFragment(fragment, false, R.anim.no_animation, R.anim.no_animation, R.anim.no_animation, R.anim.no_animation);
            }
        }
    }

@Override
    public void onFragmentBRequested() {
      FragmentB fragment = new FragmentB();
        navigateToFragment(fragment, true, R.anim.enter_from_right, R.anim.no_animation, R.anim.no_animation, R.anim.exit_from_right);
    }

onFragmentBRequested() is called from FragmentA based on a button click.

Sunny
  • 7,444
  • 22
  • 63
  • 104
  • you are not providing exit animation for a fragment (R.anim.no_animation). You should specify there animation like (R.anim.exit_to_left) – Sandip Fichadiya Oct 23 '17 at 08:14
  • I am providing the exit animation for popExit and not for normal exit. That is based on a specific requirement. The issue is during the enter animation, where Fragment B when it enters, its view enters from behind Fragment A view rather than from the front of Fragment A – Sunny Oct 23 '17 at 08:16
  • Ok so you want like fragment A should stay as it is & just the fragment B should takeover with sliding animation? – Sandip Fichadiya Oct 23 '17 at 08:17
  • no i want fragment A to go away after the sliding of fragment B is complete. This is happening as expected. The issue is that when frag B slides in, its view slide from behind the view of fragment A. I am able to notice this since half of Fragment A is transparent and Fragment B sliding in can be seen only from that transparent half indicating that it is sliding in from behind Fragment A and from from the front. – Sunny Oct 23 '17 at 08:20

1 Answers1

0

Ok, So I experimented a bit with what you are trying to do. Basically you want that current fragment shouldn't move/animate from it's place & the new fragment should animate only. I couldn't achieve with transaction.replace() with 'R.anim.no_animation' as the entry animation happens behind the current fragment.

so instead of replacing new fragment, adding it seems more practical.

so using transaction.add() instead of transaction.replace() works as expected.

Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
  • Thanks for the suggestion. The only issue is that add() would be a waste of resources as Fragment A would just be left behind and not doing anything...and the same scenario is to be done for B, C, D and E. so i would not like to have them take up resources. – Sunny Oct 23 '17 at 08:42
  • yeah this is worrying if you have series of fragments to be replaced. – Sandip Fichadiya Oct 23 '17 at 08:48