我有一个从 FragmentActivity 扩展并实现 TabHost.OnTabChangeListener 的 Main.class。我有 4 个选项卡和这个从 Fragment 扩展的调用 4 类。
例如,如果我在 tab1 的片段中并按下按钮访问 tab2,我会执行以下操作:
public class Tab01 extends Fragment {
...
Intent intent = new Intent(getActivity(), Main.class);
intent.putExtra("Screen","Tab02");
Tab01.this.startActivity(intent);
getActivity().finish();
...
}
在 Main.class :
public class Main extends FragmentActivity implements TabHost.OnTabChangeListener {
.....
private void initialiseTabHost(Bundle args) {
..
String nameTab = "";
String nameTabExtra = myIntent.getStringExtra("Screen");
if(nameTabExtra == null){
mTabHost.setCurrentTab(0);
}else{
if (nameTabExtra.equals("Tab01")){
nameTab = "Tab01";
mTabHost.setCurrentTab(0);
}else if (nameTabExtra.equals("Tab02")){
nameTab = "Tab02";
mTabHost.setCurrentTab(1);
}else if (nameTabExtra.equals("Tab03")){
nameTab = "Tab03";
mTabHost.setCurrentTab(2);
}else if (nameTabExtra.equals("Tab04")){
nameTab = "Tab04";
mTabHost.setCurrentTab(3);
}
}
this.onTabChanged(nameTab);
这可行,但这是唯一的方法吗?谢谢