Ответ 1
Есть ли способ заставить Host немедленно создать их, чтобы я мог безопасно обращаться к ним с помощью getFragmentByTag?
Нет. потому что транзакция выполняется в onAttachedToWindow()
. давайте посмотрим на исходный код:
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
String currentTab = 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 = mFragmentManager.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 = mFragmentManager.beginTransaction();
}
ft.detach(tab.fragment);
}
}
}
// We are now ready to go. Make sure we are switched to the
// correct tab.
mAttached = true;
ft = doTabChanged(currentTab, ft);
if (ft != null) {
ft.commit();
mFragmentManager.executePendingTransactions();
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mAttached = false;
}
Как вы видите, mFragmentManager.executePendingTransactions();
выполняется в onAttachedToWindow
.
Или можно создать вкладки "самостоятельно" и просто добавить их в TabHost?
да, вы можете использовать tabhost, и вы можете создать содержимое вкладки ниже.
public TabHost.TabSpec setContent (int viewId)
public TabHost.TabSpec setContent (намерение намерения)
public TabHost.TabSpec setContent (TabHost.TabContentFactory contentFactory)