I have:
- A WebViewFragment that just displays a WebView for a given URL.
- A TabbedWebViewHandler that handles creating tabs that contain WebViewFragments and adding them to the action bar.
- A HelpActivity that creates and displays 4 WebViewFragments (for “further information”, “terms and conditions”, “credits” and “purpose of this app” help screens respectively).
All of this works fine, except the first tab is always just a blank black screen when the HelpActivity first starts:

The other tabs work fine, and the first tab (in this case the “info” tab) will render it’s web view properly if another tab is selected and then the “info” tab is reselected.
I always select the first tab after I’ve created it and added it to the action bar tabs with actionBar.selectTab(newTab);. I know that the code to do this is running, because the log contains “selecting first tab”.
I’m also using the TabbedWebViewHandler for other activities that do the same thing (including for activities that only have one “tab” and so don’t display the tab navigation), so I’d prefer to fix TabbedWebViewHandler instead of putting a workaround in HelpActivity.
I’m using ActionBarSherlock / the Android support library to provide my tab functionality if that’s relevant.
How can I make sure that the first tab of my activity is always displayed properly?
WebViewFragment
public class WebViewFragment extends Fragment {
private static final String TAG = WebViewFragment.class.getSimpleName();
private String url;
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, String.format("onCreateView(): URL is '%s'", url));
View v = inflater.inflate(R.layout.snippet_webview, container, false);
WebView wv = (WebView) v.findViewById(R.id.webViewWebView);
wv.loadUrl(url);
return v;
}
public void setUrl(String url) {
this.url = url;
}
}
TabbedWebViewHandler
public class TabbedWebViewHandler {
private static final String TAG = "TabbedWebViewHandler";
private final ActionBar actionBar;
private final Context hostContext;
private final FragmentManager fragmentManager;
public TabbedWebViewHandler(SherlockFragmentActivity host) {
this.hostContext = (Context) host;
this.actionBar = host.getSupportActionBar();
this.fragmentManager = host.getSupportFragmentManager();
}
public void addTab(String title, String renderUrl) {
Tab newTab = makeTab(title, renderUrl);
actionBar.addTab(newTab);
// FIXME: buggy! tabs don't show on first load
if (actionBar.getTabCount() == 1) {
/* first tab: select it by default */
Log.d(TAG, "Selecting first tab");
actionBar.selectTab(newTab);
}
if (actionBar.getTabCount() > 1) {
/* more than one tab: enable navigating between tabs */
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
}
private Tab makeTab(String title, String renderUrl) {
WebViewFragment f =
(WebViewFragment)
SherlockFragment.instantiate(
hostContext,
WebViewFragment.class.getName());
f.setUrl(renderUrl);
ActionBar.TabListener l = new WebViewFragmentTabListener(f);
Tab newTab = actionBar.newTab();
newTab.setText(title);
newTab.setTabListener(l);
return newTab;
}
private class WebViewFragmentTabListener implements ActionBar.TabListener {
private final WebViewFragment fragment;
public WebViewFragmentTabListener(WebViewFragment f) {
this.fragment = f;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// replace current fragment with this fragment
if (ft == null) {
fragmentManager
.beginTransaction()
.replace(android.R.id.content, fragment)
.commit();
} else {
ft.replace(android.R.id.content, fragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do nothing
}
}
}
HelpActivity
public class HelpActivity extends BaseActivity {
private static final String TAG = "HelpActivity";
private static final String FURTHER_URL =
"file:///android_asset/further_info.html";
private static final String DISCLAIMER_URL =
"file:///android_asset/terms_and_conditions.html";
private static final String CREDITS_URL =
"file:///android_asset/image_credits.html";
private static final String PURPOSE_URL =
"file:///android_asset/purpose.html";
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate()");
super.onCreate(savedInstanceState);
TabbedWebViewHandler twvh = new TabbedWebViewHandler(this);
twvh.addTab("Info", FURTHER_URL);
twvh.addTab("Terms", DISCLAIMER_URL);
twvh.addTab("Credits", CREDITS_URL);
twvh.addTab("Purpose", PURPOSE_URL);
}
}
Log output
I/HelpActivity(20038): onCreate()
D/TabbedWebViewHandler(20038): Selecting first tab
D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
D/WebViewFragment(20038): onCreateView(): URL is file:///android_asset/further_info.html
I/webclipboard(20038): clipservice: android.sec.clipboard.ClipboardExManager@41d57ff8
Set the navigation mode to
ActionBar.NAVIGATION_MODE_TABSbefore you start adding tabs.edit:
Found it!
There’s a passage in action bar’s selectTab() method:
which basically prevents tab initialization if the bar is not in
ActionBar.NAVIGATION_MODE_TABS.