In my app I have set a few tabs using FragmentActivity and a button beside my tabs which activates a slide menu. At the moment I have two Fragments which are displayed for tab 1 and 2. The fragment for tab 1 contains a v2 Google Map and the second is just a blank screen.
However when I activate my slide menu I get very different behavior, I have used this slide menu lib for several projects now without any problems. When I activate slide menu while fragment 2 is displayed it works fine but when fragment 1 (map view) is displayed, activating the slide menu causes the map to go black and it displays the map again when the slide menu is closed.
Here is my code for setting up the tab
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/linearLayout1"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
</LinearLayout>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<FrameLayout
android:id="@+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</RelativeLayout>
</TabHost>
JAVA
public class TabsFragment extends Fragment implements OnTabChangeListener {
private static final String TAG = "FragmentTabs";
public static final String TAB_MAP = "Map";
public static final String TAB_VENUES = "Venus";
public static final String TAB_GENRE = "Gap";
public static final String TAB_TICKETS = "Titus";
private View mRoot;
private TabHost mTabHost;
int mCurrentTab;
Button slide_btn;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.tabs_fragment, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(0);
// manually start loading stuff in the first tab
updateTab(TAB_MAP, R.id.tab_1);
}
private void setupTabs() {
mTabHost.setup(); // important!
mTabHost.addTab(newTab(TAB_MAP, R.id.tab_1, R.drawable.tab_home));
mTabHost.addTab(newTab(TAB_VENUS, R.id.tab_2, R.drawable.tab_home));
mTabHost.addTab(newTab(TAB_GAP, R.id.tab_3, R.drawable.tab_home));
mTabHost.addTab(newTab(TAB_TITUS, R.id.tab_4, R.drawable.tab_home));
}
private TabSpec newTab(String tag, int tabContentId, int drawableId) {
Log.d(TAG, "buildTab(): tag=" + tag);
View indicator = LayoutInflater.from(getActivity()).inflate(
R.layout.tabindicator,
(ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
ImageView icon = (ImageView) indicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator(indicator);
tabSpec.setContent(tabContentId);
return tabSpec;
}
@Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
if (TAB_MAP.equals(tabId)) {
updateTab(tabId, R.id.tab_1);
mCurrentTab = 0;
return;
}
if (TAB_VENUS.equals(tabId)) {
updateTab(tabId, R.id.tab_2);
mCurrentTab = 1;
return;
}
if (TAB_GAP.equals(tabId)) {
updateTab(tabId, R.id.tab_3);
mCurrentTab = 2;
return;
}
if (TAB_TITUS.equals(tabId)) {
updateTab(tabId, R.id.tab_4);
mCurrentTab = 3;
return;
}
}
public void updateTab(String tabId, int placeholder) {
if (tabId.equals("Map")) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
getFragmentManager().beginTransaction().add(placeholder, new MyFrag(), "Map").commit();
}
} else if (tabId.equals("Venus")) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
getFragmentManager().beginTransaction().add(placeholder, new Green(), "Venus").commit();
}
} else if (tabId.equals("Gap")) {
} else if (tabId.equals("Titus")) {
}
}
public void slideMenu(){
if(isAdded()){
int width = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 200, getResources()
.getDisplayMetrics());
SlideoutActivity.prepare(getActivity(), R.id.inner_content, width);
Intent i = new Intent();
i.setClass(getActivity(), SlideMenuActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
getActivity().overridePendingTransition(0, 0);
}
}
}
Then I render this in a FragmentActivity with its own layout containing a fragment.
Here is the code I've used for the map
XML
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/relativeLayout1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<ImageButton
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/search_selected" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/button3"
android:ems="10"
android:inputType="text" >
</EditText>
</RelativeLayout>
JAVA
public class MyFrag extends Fragment {
GoogleMap map;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.main, container, false);
new LoadMap().execute();
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
public class LoadMap extends AsyncTask<String, Integer, Location> {
LocationManager lm;
LatLng p = new LatLng(51.50703296721856, -0.11260986328125);
@Override
protected Location doInBackground(String... params) {
map = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
p = new LatLng(location.getLatitude(), location.getLongitude());
return location;
}
protected void onPostExecute(Location location) {
try {
if (location != null) {
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLng(p));
map.moveCamera(CameraUpdateFactory.zoomTo(15));
map.setOnCameraChangeListener(null);
} else {
Toast.makeText(getActivity(), "You got nada",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}