duplicated id with fragment

后端 未结 3 953
情歌与酒
情歌与酒 2020-12-01 07:00

I\'m trying to applicate drawernavigation (my first fragment is a map & the others are just some fragments with simple layouts).So it runs fine & I can navigate betw

相关标签:
3条回答
  • 2020-12-01 07:31

    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;
     }
    
    0 讨论(0)
  • 2020-12-01 07:39

    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();
    }
    
    0 讨论(0)
  • 2020-12-01 07:39

    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  */
        }
    
    0 讨论(0)
提交回复
热议问题