Note that there are some explanatory texts on larger screens.

plurals
  1. POMapActivity in TabHost Fragment disappearing after tab switch
    primarykey
    data
    text
    <p>I'm trying to display a map in a set of tabs, using fragments. The problem I'm having is the map activity disappears if the user navigates to another fragment, then back. How can I display a MapActivity in fragments in a tabhost?</p> <p>The basis for this is from the numerous questions around how to integrate a MapActivity with fragments (see <a href="https://stackoverflow.com/questions/5109336/mapview-in-a-fragment-honeycomb">MapView in a Fragment (Honeycomb)</a>)</p> <p>MainTabActivity has a tabhost and uses FragmentManager to display Fragments in the tabs. MapFragment has a tabhost and uses it to display a MapActivity.<br> MapFragment does use LocalActivityManager to propogate lifecycle events to the the contained activity.</p> <p>So MapFragment:</p> <pre><code> public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.map_fragment, container, false); mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); mTabHost.setup(getLocalActivityManager()); Intent intent = new Intent(getActivity(), MapActivityWrapper.class); // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//does nothing TabHost.TabSpec tab = mTabHost.newTabSpec("map") .setIndicator("map") .setContent(intent); mTabHost.addTab(tab); return view; } </code></pre> <p>MapFragment layout:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"&gt; &lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; &lt;/LinearLayout&gt; </code></pre> <p>The code in MainTabActivity for changing the tab contents:</p> <pre><code>private void updateTab(String oldId, String tabId) { FragmentManager fm = getSupportFragmentManager(); Fragment old = fm.findFragmentByTag(oldId); Fragment selected = fm.findFragmentByTag(tabId); FragmentTransaction ft = fm.beginTransaction(); boolean added = false; if (selected == null) { selected = createFragment(tabId); } else { try { ft.remove(selected); } catch (Exception e) { ft.add(android.R.id.tabcontent, selected, tabId); added = true; //exception for removing an unadded fragment... why throw an exception? } } if (old != null) ft.remove(old); if (!added) ft.replace(android.R.id.tabcontent, selected, tabId); if (!creating &amp;&amp; !backStackProcessing) ft.addToBackStack(null);//member vars ft.commit(); } </code></pre> <p>MainTabActivity layout (deleted a bunch of unrelated layout):</p> <pre><code>&lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#EFEFEF"&gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_gravity="top" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/app_default_bg" android:layout_above="@+id/ad_region" android:layout_below="@+id/quick_action_region"&gt; &lt;FrameLayout android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;FrameLayout android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;FrameLayout android:id="@+id/map_tab" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;FrameLayout android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;/FrameLayout&gt; &lt;TabWidget android:layout_width="fill_parent" android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true"/&gt; &lt;/RelativeLayout&gt; &lt;/TabHost&gt; </code></pre> <p>Again - The problem I'm having is the map activity disappears if the user navigates to another fragment, then back. How can I display a MapActivity in fragments in a tabhost?</p> <p>Thanks much.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload