0

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
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
smriti3
  • 871
  • 2
  • 15
  • 35
  • 2
    `TabActivity` is deprecated. http://developer.android.com/reference/android/app/TabActivity.html – Raghunandan Dec 30 '13 at 06:32
  • I know tab activity is depricated .... but i am just trying to learn some basics .... how can we achieve this ! – smriti3 Dec 30 '13 at 06:34
  • 2
    i would suggest you to use fragments as suggested in the docs and as i suggested previously to your other posts use interface as a callback to the activity. from activity communicate value to other fragment – Raghunandan Dec 30 '13 at 06:36
  • 1
    But why would you want to start with deprecated thing? Instead as @Raghunandan has suggested, start with fragment, at least basics of it would help you out. – Paresh Mayani Dec 30 '13 at 06:40
  • developer basics: never use deprecated codes. – Raptor Dec 30 '13 at 06:45

1 Answers1

2
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

ashakirov
  • 12,112
  • 6
  • 40
  • 40
  • @ almaz_from_kazan ..... Thank you .... i did achieve my goal .... i have one more doubt .... you have hardcoded the String TAB_TITLE="Starters" ...... is there a way i can dynamically access the tab text ... so i could pass .... i am refering similarly to how we dynamically fetch values from edittext .... any inputs ? – smriti3 Dec 30 '13 at 08:05