简体   繁体   中英

How can I pass extras to an intent inside of a TabActivity?

I have a TabActivity that hosts many tabs. Each tab refers to the same activity, however logic is performed inside the activity that determines what to load. I would like this to be done by passing a boolean to the respective tab. How can I do this?

package com.stocktwits.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class StockTwitsTabActivity extends TabActivity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stocktwits_tab_activity);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, StreamActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        intent.putExtra("reloadAll", true);
        spec = tabHost.newTabSpec("all").setIndicator("All",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, StreamActivity.class);
        spec = tabHost.newTabSpec("home").setIndicator("Home",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, StreamActivity.class);
        spec = tabHost.newTabSpec("mentions").setIndicator("Mentions",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, StreamActivity.class);
        spec = tabHost.newTabSpec("directs").setIndicator("Directs",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, StreamActivity.class);
        spec = tabHost.newTabSpec("more").setIndicator("More",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }
}

It sounds like you're doing it wrong. Why would you want the same activity passed to all the tabs? Either use different activities, or different views. Not the same activity.

Look at here

Let me know if this is a valid solution for your purpose

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