0

I have two fragments - A and B - both fragments take up the entire screen. When I load my activity I show the user fragment A while in the background I load fragment B (in the fragmentmanager I do replace on fragment A and then on fragment B I do add and hide). Both fragments have relatively similar layouts (or at least it would be fair to say that the layout of A just before the transition is similar to the starting layout of B - views move around) and when I want to move from A to B I do a "show" on B and "hide" on A. It almost works perfectly, but you can still see a short flash of white in between, before B is shown, and it ruins the illusion that both are the same fragment. How can I transition without the user noticing?

A couple of words of explanation as to why there are two fragments and not one - the business logic is separate. Each fragment deals with a different part of the flow and it wouldn't make any sense to combine them.

Things I've tried so far:

• as I said above, I tried adding B in the background and only doing a "show" on it when it's needed - to save any setup time

• I tried overriding the pending transition and putting a fadeout/fadein, but it made no difference

• I tried hiding fragmentA instead of removing it when showing fragmentB, but it made no difference

• I tried overriding pending transition with 0,0 (getting rid of transitions entirely) but it's still the same

Code:

getSupportFragmentManager().beginTransaction()
            .replace(R.id.contentBlock, fragmentA, "FRAGMENTA")
            .add(R.id.contentBlock, fragmentB)
            .hide(fragmentB)
            .commit();

and later:

getSupportFragmentManager().beginTransaction()
            .show(fragmentB)
            .remove(fragmentA)
            .commit();
Jon
  • 7,693
  • 8
  • 47
  • 103

2 Answers2

1

Have you tried setting TRANSIT_NONE for the FragmentTransaction ?

TRANSIT_NONE No animation for transition.

Also, considering that you are just replacing one fragment with another, I suggest using replace as in the example below:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

NOTE: Fragments that are hard coded in XML, cannot be replaced. (as stated here)

Community
  • 1
  • 1
  • Both solutions work equally well and stop the screen from flickering, so I would honestly give them both best answer. Since there can only be one, though, I'm going to have to choose this one – Jon Mar 18 '16 at 13:15
1
mMapFragment = new MapFragment();

ft.beginTransaction(mMapFragment) .detach(getactivity) .attach(mMapFragment) .commit();

user5466222
  • 161
  • 1
  • 5