fragments and onConfigurationChanged

前端 未结 5 690
清歌不尽
清歌不尽 2021-02-06 07:00

I\'m trying to do something I do with activities, but within a fragment. What I do is using activities:

First stop the activity restarts when rotating the device a

5条回答
  •  日久生厌
    2021-02-06 08:07

    I could do it by re-attaching the fragment within onConfigurationChanged:

       @Override
       public void onConfigurationChanged(Configuration newConfig)
        {
            getActivity().detachFragment(this);
    
            super.onConfigurationChanged(newConfig);
    
            ....
    
            getActivity().attachFragment(this);
        }
    

    Remember that by detaching and attaching your fragment you will be only working with its view. But the fragment state is "saved" in the Fragment manager.

提交回复
热议问题