Tablet Screen Orientation Change - No View found for id for Fragment

前端 未结 3 586
温柔的废话
温柔的废话 2021-01-04 23:57

I have looked at questions related to my problem on SO but couldn\'t figure out what the problem is. Bear with me if this is a repost.

Here is what i am looking for:

相关标签:
3条回答
  • 2021-01-05 00:34

    Change the code,

    if ((findViewById(R.id.fragment_container) != null)
                && (findViewById(R.id.detail_fragment_container) != null)) {
            mTwoPane = true;
        } else {
            mTwoPane = false;
        }
    

    by

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
        //Do some stuff
    mTwoPane = true;
    } else{
     mTwoPane = false;
    }
    

    hope it will help you

    0 讨论(0)
  • 2021-01-05 00:40

    The only way I can think of you getting that error is that your old layout is used. That could be because super.onCreate(savedInstanceState);

    Instead try ignoring the saved state:

    super.onCreate(null);
    

    Edit:

    Response to @matiash comment:

    Since OP didn't provide code to re-create the problem, it's hard to test other (if any) solutions.

    However I do agree that resetting the savedInstanceState is kind of an overkill. Therefore I think OP should try it himself and see to it that he saves as many views as possible.

    The first thing that comes to mind is preventing the problematic view from being saved:

    <FrameLayout
        android:id="@+id/detail_fragment_container"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="0.60"
        android:saveEnabled="false"/>
    
    0 讨论(0)
  • 2021-01-05 00:54

    The FragmentManager will try to recreate all the fragments when rotated, but in portrait mode you don't have the DetailFragment layout anymore, so you should remove the fragment to prevent it from being attached.

    EDIT: Perhaps the cleanest way to detect your orientation change is to use the OrientationEventListener. You can refer to this post and this for some example. You should remove the fragment inside the method onOrientationChanged (int orientation) which you should override.

    0 讨论(0)
提交回复
热议问题