Update API demos to match new tab interaction.
Follow changes to FragmentTabManager to continue to work correctly with it. Fix the code in the base API demo (which can't use FragmentTabManager) to work correctly. Bug #7232088: ListView saved state being lost in some cases Change-Id: Ib7ba1ab20ff00f2cc95c9f6024ab94783237c9be
This commit is contained in:
@@ -20,6 +20,7 @@ import android.app.ActionBar;
|
||||
import android.app.ActionBar.Tab;
|
||||
import android.app.Activity;
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
@@ -31,6 +32,7 @@ import android.widget.Toast;
|
||||
public class FragmentNestingTabs extends Activity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
FragmentManager.enableDebugLogging(true);
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final ActionBar bar = getActionBar();
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package com.example.android.apis.app;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
|
||||
@@ -29,28 +29,32 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TabHost;
|
||||
|
||||
/**
|
||||
* Sample fragment that contains tabs of other fragments.
|
||||
*/
|
||||
public class FragmentTabsFragment extends Fragment {
|
||||
TabHost mTabHost;
|
||||
TabManager mTabManager;
|
||||
String mCurrentTabTag;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mTabManager = new TabManager(getActivity(), getChildFragmentManager(),
|
||||
R.id.realtabcontent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View v = inflater.inflate(R.layout.fragment_tabs_fragment, container, false);
|
||||
mTabHost = (TabHost)v.findViewById(android.R.id.tabhost);
|
||||
mTabHost.setup();
|
||||
TabHost host = mTabManager.handleCreateView(v);
|
||||
|
||||
mTabManager = new TabManager(getActivity(), getChildFragmentManager(),
|
||||
mTabHost, R.id.realtabcontent);
|
||||
|
||||
mTabManager.addTab(mTabHost.newTabSpec("result").setIndicator("Result"),
|
||||
mTabManager.addTab(host.newTabSpec("result").setIndicator("Result"),
|
||||
FragmentReceiveResult.ReceiveResultFragment.class, null);
|
||||
mTabManager.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
|
||||
mTabManager.addTab(host.newTabSpec("contacts").setIndicator("Contacts"),
|
||||
LoaderCursor.CursorLoaderListFragment.class, null);
|
||||
mTabManager.addTab(mTabHost.newTabSpec("apps").setIndicator("Apps"),
|
||||
mTabManager.addTab(host.newTabSpec("apps").setIndicator("Apps"),
|
||||
LoaderCustom.AppListFragment.class, null);
|
||||
mTabManager.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
|
||||
mTabManager.addTab(host.newTabSpec("throttle").setIndicator("Throttle"),
|
||||
LoaderThrottle.ThrottledLoaderListFragment.class, null);
|
||||
|
||||
return v;
|
||||
@@ -59,47 +63,37 @@ public class FragmentTabsFragment extends Fragment {
|
||||
@Override
|
||||
public void onViewStateRestored(Bundle savedInstanceState) {
|
||||
super.onViewStateRestored(savedInstanceState);
|
||||
if (savedInstanceState != null) {
|
||||
mCurrentTabTag = savedInstanceState.getString("tab");
|
||||
}
|
||||
mTabHost.setCurrentTabByTag(mCurrentTabTag);
|
||||
mTabManager.handleViewStateRestored(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
// Need to remember the selected tab so that we can restore it if
|
||||
// we later re-create the views.
|
||||
mCurrentTabTag = mTabHost.getCurrentTabTag();
|
||||
mTabHost = null;
|
||||
mTabManager = null;
|
||||
mTabManager.handleDestroyView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putString("tab", mTabHost != null
|
||||
? mTabHost.getCurrentTabTag() : mCurrentTabTag);
|
||||
mTabManager.handleSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a helper class that implements a generic mechanism for
|
||||
* associating fragments with the tabs in a tab host. It relies on a
|
||||
* trick. Normally a tab host has a simple API for supplying a View or
|
||||
* Intent that each tab will show. This is not sufficient for switching
|
||||
* between fragments. So instead we make the content part of the tab host
|
||||
* 0dp high (it is not shown) and the TabManager supplies its own dummy
|
||||
* view to show as the tab content. It listens to changes in tabs, and takes
|
||||
* care of switch to the correct fragment shown in a separate content area
|
||||
* whenever the selected tab changes.
|
||||
* associating fragments with the tabs in a tab host. DO NOT USE THIS.
|
||||
* If you want tabs in a fragment, use the support v13 library's
|
||||
* FragmentTabHost class, which takes care of all of this for you (in
|
||||
* a simpler way even).
|
||||
*/
|
||||
public static class TabManager implements TabHost.OnTabChangeListener {
|
||||
private final Context mContext;
|
||||
private final FragmentManager mManager;
|
||||
private final TabHost mTabHost;
|
||||
private final int mContainerId;
|
||||
private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
|
||||
TabInfo mLastTab;
|
||||
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
|
||||
private TabHost mTabHost;
|
||||
private TabInfo mLastTab;
|
||||
private boolean mInitialized;
|
||||
private String mCurrentTabTag;
|
||||
|
||||
static final class TabInfo {
|
||||
private final String tag;
|
||||
@@ -130,40 +124,109 @@ public class FragmentTabsFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
public TabManager(Context context, FragmentManager manager, TabHost tabHost,
|
||||
int containerId) {
|
||||
public TabManager(Context context, FragmentManager manager, int containerId) {
|
||||
mContext = context;
|
||||
mManager = manager;
|
||||
mTabHost = tabHost;
|
||||
mContainerId = containerId;
|
||||
}
|
||||
|
||||
public TabHost handleCreateView(View root) {
|
||||
if (mTabHost != null) {
|
||||
throw new IllegalStateException("TabHost already set");
|
||||
}
|
||||
mTabHost = (TabHost)root.findViewById(android.R.id.tabhost);
|
||||
mTabHost.setup();
|
||||
mTabHost.setOnTabChangedListener(this);
|
||||
return mTabHost;
|
||||
}
|
||||
|
||||
public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
|
||||
tabSpec.setContent(new DummyTabFactory(mContext));
|
||||
String tag = tabSpec.getTag();
|
||||
|
||||
TabInfo info = new TabInfo(tag, clss, args);
|
||||
mTabs.add(info);
|
||||
mTabHost.addTab(tabSpec);
|
||||
}
|
||||
|
||||
// Check to see if we already have a fragment for this tab, probably
|
||||
// from a previously saved state. If so, deactivate it, because our
|
||||
// initial state is that a tab isn't shown.
|
||||
info.fragment = mManager.findFragmentByTag(tag);
|
||||
if (info.fragment != null && !info.fragment.isDetached()) {
|
||||
FragmentTransaction ft = mManager.beginTransaction();
|
||||
ft.detach(info.fragment);
|
||||
ft.commit();
|
||||
public void handleViewStateRestored(Bundle savedInstanceState) {
|
||||
if (savedInstanceState != null) {
|
||||
mCurrentTabTag = savedInstanceState.getString("tab");
|
||||
}
|
||||
mTabHost.setCurrentTabByTag(mCurrentTabTag);
|
||||
|
||||
String currentTab = mTabHost.getCurrentTabTag();
|
||||
|
||||
// Go through all tabs and make sure their fragments match
|
||||
// the correct state.
|
||||
FragmentTransaction ft = null;
|
||||
for (int i=0; i<mTabs.size(); i++) {
|
||||
TabInfo tab = mTabs.get(i);
|
||||
tab.fragment = mManager.findFragmentByTag(tab.tag);
|
||||
if (tab.fragment != null && !tab.fragment.isDetached()) {
|
||||
if (tab.tag.equals(currentTab)) {
|
||||
// The fragment for this tab is already there and
|
||||
// active, and it is what we really want to have
|
||||
// as the current tab. Nothing to do.
|
||||
mLastTab = tab;
|
||||
} else {
|
||||
// This fragment was restored in the active state,
|
||||
// but is not the current tab. Deactivate it.
|
||||
if (ft == null) {
|
||||
ft = mManager.beginTransaction();
|
||||
}
|
||||
ft.detach(tab.fragment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mTabs.put(tag, info);
|
||||
mTabHost.addTab(tabSpec);
|
||||
// We are now ready to go. Make sure we are switched to the
|
||||
// correct tab.
|
||||
mInitialized = true;
|
||||
ft = doTabChanged(currentTab, ft);
|
||||
if (ft != null) {
|
||||
ft.commit();
|
||||
mManager.executePendingTransactions();
|
||||
}
|
||||
}
|
||||
|
||||
public void handleDestroyView() {
|
||||
mCurrentTabTag = mTabHost.getCurrentTabTag();
|
||||
mTabHost = null;
|
||||
mTabs.clear();
|
||||
mInitialized = false;
|
||||
}
|
||||
|
||||
public void handleSaveInstanceState(Bundle outState) {
|
||||
outState.putString("tab", mTabHost != null
|
||||
? mTabHost.getCurrentTabTag() : mCurrentTabTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTabChanged(String tabId) {
|
||||
TabInfo newTab = mTabs.get(tabId);
|
||||
if (!mInitialized) {
|
||||
return;
|
||||
}
|
||||
FragmentTransaction ft = doTabChanged(tabId, null);
|
||||
if (ft != null) {
|
||||
ft.commit();
|
||||
}
|
||||
}
|
||||
|
||||
private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
|
||||
TabInfo newTab = null;
|
||||
for (int i=0; i<mTabs.size(); i++) {
|
||||
TabInfo tab = mTabs.get(i);
|
||||
if (tab.tag.equals(tabId)) {
|
||||
newTab = tab;
|
||||
}
|
||||
}
|
||||
if (newTab == null) {
|
||||
throw new IllegalStateException("No tab known for tag " + tabId);
|
||||
}
|
||||
if (mLastTab != newTab) {
|
||||
FragmentTransaction ft = mManager.beginTransaction();
|
||||
if (ft == null) {
|
||||
ft = mManager.beginTransaction();
|
||||
}
|
||||
if (mLastTab != null) {
|
||||
if (mLastTab.fragment != null) {
|
||||
ft.detach(mLastTab.fragment);
|
||||
@@ -180,9 +243,8 @@ public class FragmentTabsFragment extends Fragment {
|
||||
}
|
||||
|
||||
mLastTab = newTab;
|
||||
ft.commit();
|
||||
mManager.executePendingTransactions();
|
||||
}
|
||||
return ft;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user