Ji Ya Ji Ya - 15 days ago 6
Android Question

Blank page in FragmentTabHost with AsyncTask

I'm new to Android and developing a FragmentTabHost page with 3 tabs. In the activity class, call an AsyncTask class to fetch data and put into Shared Preferences, then have the following code to add tabs. In each tab fragment class, get saved shared preferences data and display on page.

mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

mTabHost.addTab(mTabHost.newTabSpec("A").setIndicator("A"),
AFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("B").setIndicator("B"),
BFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("C").setIndicator("C"),
CFragment.class, null);


The problem is that the first shown tab page is blank. Because it gets created before the async task has finished. Therefore, the information in the tab is shown only after changing tabs and reselecting it again. Any good solutions?

Much appreciated!

Answer

I found a workable solution. Call the async task in AFragment, put data in shared preference, then update UI. Another two fragments will read data from shared preferences directly.

Comments