Switching orientation error: Fragment Error - Duplicate id, tag, or parent id 0x0

北战南征 提交于 2019-12-05 14:37:15

Got it:

The bug was with the holder activity for SearchPageFragment adding the Fragment twice.

Android will always retain fragments attached to a View when an orientation change occurs.

Because of this, you need to ensure that if you're adding a Fragment in your onCreate() method you surround it's creation (and addition/replacement transation) with an if statement to check that savedInstanceState is null (If it's not null it indicates an orientation change has occurred).

if(savedInstanceState == null) {
    // Add fragment code here
}

I solved the problem using this .

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         if (root != null) {
                ViewGroup parent = (ViewGroup) root.getParent();
                if (parent != null)
                    parent.removeView(root);
            }
            try {
                root = inflater.inflate(R.layout.activity_explore,container,false);
            } catch (InflateException e) {
                /* map is already there, just return view as it is */
            }
 return root;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!