Question

What i am trying to do::

enter image description here

BreakfastLunchDinnerIndividualListOfItems.java

public class BreakfastLunchDinnerIndividualListOfItems extends TabActivity implements OnTabChangeListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.breakfast_lunch_dinner_individual_list_of_items);

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

        String REST = getTabHost().toString();

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);
        //intent.putExtra("Starters", REST);
        spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs

        intent = new Intent().setClass(this, BLD_IndividualListOfItems_MainCourse.class);
        //intent.putExtra("MainCourse", REST);
        spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);
        tabHost.addTab(spec);
    }

    public void onTabChanged(String arg0)
    {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(),arg0, Toast.LENGTH_LONG).show();
    }
}

  • I tried with String REST = getTabHost().toString(); problem is that on click of tab i am not able to send perticular tabtext to the activity that the tab launches
  • I know tab activity is depricated, i am just learning
  • How to resolve this, hope i am clear
Was it helpful?

Solution

spec = tabHost.newTabSpec("Starters").setIndicator("Starters").setContent(intent);
spec = tabHost.newTabSpec("MAIN_COURSE").setIndicator("Main Course").setContent(intent);

here Starters and Main Course are the titles of tabs. The easiest way to send this strings to bounded activities is to send them throught intents which are bounded to corresponding tabSpecs.

    String TAB_TITLE="Starters";
    Intent intent = new Intent().setClass(this, BLD_IndividualListOfItems_Starters.class);

    Bundle bundle =new Bundle();
    bundle.putString("key_title", TAB_TITLE);
    intent.putExtras(bundle);

    spec = tabHost.newTabSpec("Starters").setIndicator(TAB_TITLE).setContent(intent);
    tabHost.addTab(spec);

And here is how you can get this string in BLD_IndividualListOfItems_Starters activity :

protected void onCreate(Bundle savedInstanceState) {
     ...
     String title=getIntent().getExtras().getString("key_title");
}

But as Raghunandan says, it's better to use fragments instead of depracated TabActivity.

EDITED:

if you want to send text to corresponding activity dinamically, i mean exactly after tab is changed - you can:

  1. create and broadcast custom intent with your string. (CUSTOM_INTENT_EXAMPLE)
  2. in BLD_IndividualListOfItems_Starters activity register BroadcastReciever which will catch your custom intent and take text from it. (BROADCAST_RECIEVER_FROM_ACTIVITY_EXAMPLE)

You can't directly access tab text from BLD_IndividualListOfItems_Starters, because TabActivity and BLD_IndividualListOfItems_Starters are two different activities. But you can send data between activities via bundles, static fields, singletons etc. Here is link to docs http://developer.android.com/guide/faq/framework.html#3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top