duplicated id with fragment

浪子不回头ぞ 提交于 2019-11-27 04:01:17
Hardik

use this

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
         />

and override method onDestroyView() and Just put this code in onDestroyView()

public void onDestroyView() 
 {
    super.onDestroyView(); 
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
}

Try to reuse/recycle your layout. I am running into "duplicate id" when using a map fragment. So in onCreateView instead of

final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

i am using

public class YourFragment extends Fragment {
   public YourFragment(){}
   ...
   private static View rootView;
   ...


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //View rootView = inflater.inflate(R.layout.fragment_layout, container, false);

    if (rootView != null) {
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null)
            parent.removeView(rootView);
    }
    try {
        rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is  */
    }

I think you have to try to retrieve old Fragments instance instead of recreating it each time a drawer item is selected. In your displayView method of LoginScreen Activity, you have to do something like that in the switch:

Fragment fragment = null;
String title = getResources().getString(SOME_FRAGMENT_TITLE);
switch (position) {
case 1:
   fragment = (YourFragment) fm.findFragmentByTag(title);
   if (fragment == null) fragment = new YourFragment();
   break;
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!