0

In a master detail flow, when I go from landscape to portrait, my detail fragment is still there.

What's the best place and time (lifecycle callback) to get rid of it? I only have to get rid of it because my menu items and actionbar title are coming from the detail fragment, in portrait mode, and so it doesn't make any sense.

EGHDK
  • 17,818
  • 45
  • 129
  • 204

2 Answers2

0

In the onCreate method of your activity, you could try that:

DetailFragment detailFrag = getFragmentManager().findFragmentByTag
if( <your logic to see if portrait> && detailFrag !=null && detailFrag.isVisible()){
    <Remove the Fragment using a normal fragment transaction>
}
JDenais
  • 2,956
  • 2
  • 21
  • 30
  • I think you misinterpreted my question. I don't need my detail fragment when I'm in portrait mode. I don't have it in portrait mode normally, but if I go to landscape, click a list item from the master fragment, it adds a detail fragment, and then if I rotate back, the detail fragment still belongs to my activity. – EGHDK Sep 11 '15 at 01:01
  • So I think I did this, and the menu items seemed to have gone away, but the action bar title is still residual from the fragment I removed. Is the action bar a separate state that gets restored? – EGHDK Sep 11 '15 at 04:28
0

Short answer would be onCreate

And there are multiple things you would need to know

  1. When screen orientation change, which is one of the configuration changes, which will trigger Activity recreation.
  2. You can stop Activity recreation by setting onConfigChange, but do NOT do this unless this is a special app, e.g. Camera
  3. Activity recreate is trying to help you, and your case is exactly where it is needed, and you can do the correct setup in onCreate
  4. There are multiple ways to handle the different orientation, the basic way would be to use different layout, e.g. for your layout, say activity_main.xml, you can define a similar xml with the same name, under layout-land folder. When you setContentView(R.layout.activity_main), Android will select the right layout for you depending on your configuration.
  5. In your case, you are switching between 2 fragments and 1 fragment, there are additional works you need to handle for the fragment transaction. However, this would depends on your existing FragmentTransaction and this will go too broad to go on, so I will stop here.
Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • So I think I did this, and the menu items seemed to have gone away, but the action bar title is still residual from the fragment I removed. Is the action bar a separate state that gets restored? – EGHDK Sep 11 '15 at 04:28
  • How do you control your action bar title? by `setTitle` programmatically in Fragment ? If so, of course it would not automatically revert when you remove your Fragment – Derek Fung Sep 11 '15 at 04:31
  • You should just `setTitle` to the correct one in `onCreate`. Given that you have handled the fragment correctly – Derek Fung Sep 11 '15 at 04:35