2

in my Activity, I have a layout containing 3 FrameLayouts, one at the top, one at the left and one at the "center".

Now, I sometimes only want to display one or two of them. Atm I am doing it this way:

FrameLayout frame = (FrameLayout) findViewById(R.id.framelayout_menu_left);
frame.setVisibility(...);

frame = (FrameLayout) findViewById(R.id.framelayout_content);
frame.setVisibility(...);

frame = (FrameLayout) findViewById(R.id.framelayout_menu_top);
frame.setVisibility(...);

However this can get really ugly results, e.g. when I switch the "content" Fragment and hide the top and/or left FrameLayout. It all starts flickering as the "content" Fragment jumps to the top and/or left and only afterwards is replaced.

Also, I can obviously not navigate back to another setup, so is there any other way to do this?

Kind regards, jellyfish

Edit:

Maybe a little drawing makes my question clearer...

Layout

A shows a Layout of 3 FrameLayouts containing 3 different Fragments. Each color represents one distinct Fragment.

Now what I want to do is to switch from A to D. I am doing this by replacing the blue Fragment with the yellow Fragment via a FragmentTransaction.

However, this still keeps the other Frames visible, so I hide them via the code above.

Now, Frame.setVisibility() is called way before commit(), so in B and C the blue Fragment "jumps" to the left and the top and only afterwards (in D) is replaced with the yellow Fragment. This produces a nasty flickering.

As a workaround, I now hide all three FrameLayouts before the transaction and re-show the ones I need once the transaction has finished. But there still is the problem that I can't go back via the back button as this isn't a real transaction.

jellyfish
  • 7,868
  • 11
  • 37
  • 49

1 Answers1

3

I would have two suggestions. Firstly, if you both add a fragment transition effect and do the visibility changes after the transaction, that would probably substantially reduce much of your flicker effect

ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

Secondly, I've simply given up on having the system manage the fragment stack for me -- it seems that this only works well with simple transactions. Override onBackPressed and do your own logic there.

--randy

wilbur4321
  • 855
  • 6
  • 10
  • Hi randy, thanks for your answer. :) On the first part, I had this idea, too, but got stuck on the "after the transaction" bit. It's really unhandy that after commit() the transaction doesn't take effect immediately. I came up to doing the hiding/showing in the Fragment itself now (where I can be sure the transaction is done). Your second idea sounds great. I didn't run into more problems with the fragment stack management so far, but easily believe you that it doesn't always work 100%. Didn't even think of such a "simple" solution, thanks! – jellyfish Jun 20 '11 at 07:58
  • 1
    @jellyfish -- I found out something additional yesterday which might really help -- LayoutTransitions. This allows you to define a specific animation to occur when a layout changes; you could perhaps use this to add a nice slide-away effect on the transition when views are hidden. See http://developer.android.com/guide/topics/graphics/animation.html#layout for documentation -- this could be as simple as a single xml change for you. – wilbur4321 Jun 21 '11 at 01:11