开发者

Passing data from tabhost activity to its inner activity

开发者 https://www.devze.com 2023-03-07 03:35 出处:网络
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.

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 开发者_如何学JAVAfrom 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号