-1

I have a tab activity that every of my applications tabs are opening the same activity lets say SecondActivity.So the code is as shown below.

TabSpec firstTab = tabHost.newTabSpec("tid1");
 firstTab.setIndicator("First Tab Name").setContent(new Intent(this,SecondActivity..class));
 tabHost.addTab(firstTab);

 TabSpec secondTab = tabHost.newTabSpec("tid1");
 firstTab.setIndicator("Second Tab Name").setContent(new Intent(this,SecondActivity.class));
 tabHost.addTab(secondTab);

now I want to pass some data from my tab activity every time the SecondActivity activity is called. So I tried it this way but it didnt seem to work:

TabSpec firstTab = tabHost.newTabSpec("tid1");
  Intent intent = new Intent(this, SecondActivity.class);
  Bundle b1 = new Bundle();
  b1.putString("name","Something");

 firstTab.setIndicator("First Tab Name").setContent(intent);
 tabHost.addTab(firstTab);


 TabSpec secondTab = tabHost.newTabSpec("tid1");
  Intent intent = new Intent(this, SecondActivity.class);
  Bundle b2 = new Bundle();
  b2.putString("name","Something2");

secondTab.setIndicator("First Tab Name").setContent(intent);
 tabHost.addTab(SecondTab);

Does anyone have any idea if it should work in this way or if is it possible to do it in any other way?? In other worlds I want the second activity to know which of the tabs is pressed

George32x
  • 37
  • 4
  • 7

2 Answers2

3

You could do something like this,

TabSpec firstTab = tabHost.newTabSpec("tid1");
Intent intent = new Intent(this, SecondActivity.class);
intent.put("name", "Something1");

firstTab.setIndicator("First Tab Name").setContent(intent);
tabHost.addTab(firstTab);

From the SecondActivity, in the onCreate method, you could call getIntent(), and then extract the information.

varuaa
  • 389
  • 2
  • 9
  • 1
    Thats exactly what I was using for.Actualy the function name is intent.putExtra("name1","value") thanks both. – George32x May 20 '11 at 19:02
  • @George32x, you're welcome. Please don't forget to select one of the answers if they helped. – bursk May 20 '11 at 20:33
  • This was exactly what I was looking for. I feel stupid for not thinking aobut this myself, but oh well. They need to update the Tabhost/tab widgets to accept extras within the constructors. Or add a method to take an extra. I hate having to use a different intent for each tab. I woudl like to have just 1. But, this will serve its purpose – Shaun Dec 31 '11 at 22:21
0

I'm not sure I follow what you are trying to ask, but it is possible to pass data (of a variety of type) to the Activity classes that make up the Tabs of your TabHost. For an example you can check out this thread (Android - creating a Generic TabHost, passing info using Bundle problem) that discusses how to setup a TabHost that then creates Activity based Tabs, passing data into them if needed.

Just noticed the note from varuaa. Yes, the bundles don't do any good if nothing is added to the Intent. Check out intent.putExtra for a wide variety of items that can be added easily. I make use of classes that implement Serializable all the time to pass data into a new Activity.

Community
  • 1
  • 1
bursk
  • 1,647
  • 7
  • 25
  • 39
  • This might be going off-topic, but is it a good practice to pass Serializable classes through intents. I had to implement something like that, and all over the net people discouraged using it, and Parcelable was said to be the way to go. – varuaa May 20 '11 at 18:48
  • @varuaa, you're right, that seems to be a debate unto itself. From a high level it sounds like the argument against Serializable is that its slower than Parcelable. This thread (http://stackoverflow.com/questions/5550670/benefit-of-using-parcelable-instead-of-serializing-object) seems to have some good info. I may need to relook at my usage and see if Parcelable would work out better. – bursk May 20 '11 at 20:31