Fragments Not Working Correctly After Orientation Change

后端 未结 5 1258
失恋的感觉
失恋的感觉 2020-12-14 10:36

I am having a problem with Fragments and orientation change.

I have an application which has a MainActivity which handles the switching up

5条回答
  •  爱一瞬间的悲伤
    2020-12-14 11:07

    if(savedInstanceState == null) is not working always. sometimes savedInstanceState != null while you need to add another fragment because of landscape orientation.

    Another approach is to test whether fm.findFragmentById(R.id.frameLayoutLeft), no matter what the orientation is, is null or not, if so, than create a new fragment instance otherwise do nothing. If you need a second fragment in landscape mode, you need first check whether it's landscape or not if so, check whether fm.findFragmentById(R.id.frameLayoutRight) is null or not. if null, than create, otherwise do nothing because it's already there retained by Android OS.

提交回复
热议问题