Replacing fragments and orientation change

﹥>﹥吖頭↗ 提交于 2019-11-30 01:52:19

Don't create the SearchFormFragment in XML. Instead have an empty FrameLayout which you populate in Activity.onCreate() without adding it to the back stack. This way the Activity will keep the current Fragment instead of trying to add the one specified in XML.

Also, using AsyncLoader may be a better approach, see http://code.google.com/p/android/issues/detail?id=14944

Henry

After struggling with this for many hours, I finally got it. The problem is not in the setup or the way we call Fragment class at all. It has to do with a wrong message being displayed on the screen. If you have a check for a container being null in your onCreateView() in your Ftagment class, you will get that "unable to inflate fragment" message, instead of container is null message. So, do not check for null container in your onCreateView(). So make sure your onCreateView() looks like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return(inflater.inflate(R.layout.title_layout, container, false));
    }

and NOT like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(container == null){
            return null;                                
        }
        return(inflater.inflate(R.layout.title_layout, container, false));
    }

For reference reasons: We were able to solve a very similar problem by adding an android:id to our fragment created in XML.

This very important precondition for supporting crash-free rotation is only mentioned as a side-note in the documentation:

Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!