fragments and onConfigurationChanged

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 03:25:10

If I understand correctly, you don't want to the fragment to reload on each rotation. Instead you want to relayout the views and update them with information you already have.

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Get a layout inflater (inflater from getActivity() or getSupportActivity() works as well)
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newView = inflater.inflate(R.layout.frg.myFragment, null);
    // This just inflates the view but doesn't add it to any thing.
    // You need to add it to the root view of the fragment
    ViewGroup rootView = (ViewGroup) getView();
    // Remove all the existing views from the root view.
    // This is also a good place to recycle any resources you won't need anymore
    rootView.removeAllViews();
    rootView.addView(newView);
    // Viola, you have the new view setup
}

According to the documentation (getView()), getView() returns the same view that you returned from your onCreateView() but it does not. It actually return the parent of the view you returned in onCreateView() which is exactly what you need. getView() will return an instance of NoSaveStateFrameLayout, which is used specifically for Fragments as its root view.

Hope this helps.

Have you considered retaining your fragment instances? See Fragment#setRetainInstance.

Allow your Activity to be recreated (do not specify android:configChanges) but retain your fragment instances across orientation changes. If all the heavy lifting happens in Fragment#onCreate this should work fine. onCreate() will not be called again since the fragment is not being re-created.

TatoCuervo

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.

Maybe you can try use

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.frg.myFragment, container, false);
}

. In any case, the fragment still has to be destroyed and recreated, why not let Android handle it automatically by restarting the activity? If there is any data to keep, you can save it in onSavedInstanceState(). Setting android:configChanges="keyboardHidden|orientation|screenSize" is not recommended in Android.

You can try to detach e attach the fragment:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager != null) {
        fragmentManager.beginTransaction().detach(this).commitAllowingStateLoss();
    }
    super.onConfigurationChanged(newConfig);
    if (fragmentManager != null) {
        fragmentManager.beginTransaction().attach(this).commitAllowingStateLoss();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!