简体   繁体   中英

Creating Tabhost and Adding specs from AsyncTask in Android

I am trying to create a tabbar and specs in it , but the thing is I need to do it in an asycn task so its without TabActivity. After running this code below I only see my main layout. And in the end what Im trying to do is, I wanna show a progress bar to the user which say the necessary files are getting copied just for the first time once application is installed, in the mean time copy files then create tabHost and add specs. Without an asyncTask it works quite good but for the first time application runs the screen gets locked for 20 or 30 seconds till the copying process get done. Any idea ? Thanks in advance.

public class AsyncTest extends AsyncTask<Void, Void, Void> {

Context context;
DataBaseJSONFunctions json;

TabHost tabHost;

TabWidget tabWidget;

Resources res;

TabHost.TabSpec sp;

Intent intent;

ProgressDialog dialog;

Activity ac;

public AsyncTest(Context context, TabHost tabHost, TabHost.TabSpec sp, Bundle savedInstanceState) {
    this.context = context;
    json = new DataBaseJSONFunctions(context);
    this.tabHost = tabHost;
    tabWidget = tabHost.getTabWidget();
    this.sp = sp;
    ac = (Activity) context;
    res = context.getResources();

    LocalActivityManager mlam = new LocalActivityManager(ac, false);
    mlam.dispatchCreate(savedInstanceState);
    tabHost.setup(mlam );

}

@Override
protected Void doInBackground(Void... params) {
    initializeAll();
    return null;
}

@Override
protected void onPostExecute(Void result) {

    dialog.dismiss();

    // to go through to the another activity in the tab I need to initialize an intent.
    // and I need to set the Tab bar and it's icon.
    intent = new Intent().setClass(ac, Activities.class);
    sp = tabHost.newTabSpec("activities").setIndicator("activities",res.getDrawable(R.drawable.tab_activities_selector)).setContent(intent);
    tabHost.addTab(sp);

    // doing the same things for Songs Activity.
    intent = new Intent().setClass(ac, Promotions.class);
    sp = tabHost.newTabSpec("promotions").setIndicator("promotions",res.getDrawable(R.drawable.tab_promotions_selector)).setContent(intent);
    tabHost.addTab(sp);


    // doing the same things for another Activity.
    intent = new Intent().setClass(ac,Menu.class);
    sp = tabHost.newTabSpec("menu").setIndicator("Menu",res.getDrawable(R.drawable.tab_menu_selector)).setContent(intent);
    tabHost.addTab(sp);


    intent = new Intent().setClass(ac, Gallery.class);
    sp = tabHost.newTabSpec("gallery").setIndicator("Gallery",res.getDrawable(R.drawable.tab_gallery_selector)).setContent(intent);
    tabHost.addTab(sp);


    intent = new Intent().setClass(ac, Info.class);
    sp = tabHost.newTabSpec("info").setIndicator("Info",res.getDrawable(R.drawable.tab_info_selector)).setContent(intent);
    tabHost.addTab(sp);

    for(int i = 0; i < tabWidget.getChildCount(); i++){
        tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bar);
    }

    tabHost.setCurrentTab(0);

    // Starting location listener service.
    ac.startService(new Intent(ac, LocationService.class));

    ac.setContentView(R.layout.tabbar_main);
    super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
    dialog = ProgressDialog.show(context, "", "Copying files please wait...");
    super.onPreExecute();
}

for tab layout your xml should b like this;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0" />
        </LinearLayout>
    </TabHost>


</RelativeLayout>

I have found the way to do it, I have an image view which has visibility as GONE and In the onPreExecute() I'm setting the visibility of this image as VISIBLE. and Also onPostExecute() I'm making it GONE as like before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM