问题
I have 2 activity: A, B. The first is an TabActivity and the second an Activity. Inside A i have a clickable button that make an intent to call the other activity.
public class A extends TabActivity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("a").setContent(R.id.a).setIndicator("a"));
b = new Intent(this,B.class);
tabHost.addTab(tabHost.newTabSpec("b").setContent(b)
.setIndicator(this.getString(R.string.b)));
btn.setOnClickListener(this);
}
public void onClick(View arg0) {
if (arg0 == btn) {
startActivity(b);
}
}
}
Inside B, i have a thread that gets data from internet every time that the button in A class is clicked:
public class B extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
searchHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
draw(msg.obj.toString());
}
}
Intent intent = getIntent();
if(..)
sendHttpRequest();
}
Data returns correctly and I can manage it, but my tabhost disappear. How I can solve?
回答1:
The android way to automatically change tab is:
tabHost.setCurrentTab(tab);
and not using:
startActivity(intent);
To use it on all your Activity the best way is to set your TabHost public and static.
回答2:
whenever you click a button you call an activity which is not child activity of tab activity. so tabhost is disapear. call activity b in tab activity just like this
tabHost.addTab(tabHost.newTabSpec("Tab")
.setIndicator("Tab")
.setContent(new Intent(this, b.class)
));
来源:https://stackoverflow.com/questions/10437181/tabactivity-that-create-an-intent-onclick-button-but-the-tabhost-of-activity-rec