Well I have a simple scenario here .
Case
1) A FragmentStateAdapter that would house three fragments
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new LocationFragment();
break;
case 1:
fragment=new CongestFrag();
break;
case 2:
fragment=new SubcribedFrag();
break;
default:
break;
}
return fragment;
}
The location Fragment as the name implies , would house a google map fragment . The XML for this would be
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/off_white"
tools:context=".MainActivity" >
<TextView
android:id="@+id/fragment_title"
android:layout_width="@dimen/fragments_title_view_width"
android:layout_height="@dimen/fragments_title_view_height"
android:text="@string/location_fragment_title"
android:textSize="@dimen/fragments_title_text_size"
android:fontFamily="sans-serif-light"/>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
Similarly the other fragments contain for example just one textviews.
The issue
We all know that when we proceed or retreat in the list of fragments , the viewpager niftly destroys and recreates fragments . THIS IS FINE for me . I do not wish to use setOffscreenPageLimit to force the fragments to stay in memory .
The problem is with the LocationFragment. The application starts with LocationFragment , which properly displays a map . This is how I proceed then
LocationFragment-->CongestFrag-->SubcribedFrag
Everything works fine . Now when I come in reverse .
SubscribedFrag->CongestFrag . The program breakpoint for OnCreateView for LocationFragment hits and it gives this stack trace
10-12 15:26:21.096: E/AndroidRuntime(17122): Caused by: java.lang.IllegalArgumentException: Binary XML file line #16: Duplicate id 0x7f060010, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
This is the code that breaks (LocationFragment.java), when I come towards LocationFragment from the bottom of the fragment list
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
mFragmentView =inflater.inflate(R.layout.location_fragment, container, false);
//code breaks above
mMap.addMarker(new MarkerOptions().position(new LatLng(Globals.LocationFragment.DEFAULT_LAT, Globals.LocationFragment.DEFAULT_LONG)).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Globals.LocationFragment.DEFAULT_LAT, Globals.LocationFragment.DEFAULT_LONG), 8.0f));
/*Just a container to keep the width and height . See usage*/
mSize = new Point();
initProperties();
initControls();
return mFragmentView;
}
Speculation It seems that the fragment is previously not properly destroyed , and android is inflating the same view twice . But that doesn't make sense ofcourse . Please aid me in this .
Thanks