就我而言,我在 TabActivity 中存储了对意图的引用
mGalleryTabIntent = new Intent(this, AnActivity.class);
spec = getTabHost().newTabSpec(TAB_GALLERY).setIndicator(res.getString(R.string.footer_gallery),res.getDrawable(R.drawable.gallery_icon_sel)).setContent(mGalleryTabIntent);
public Intent getStoredTabIntent(){
return mGalleryTabIntent;
}
然后,当我想从一个孩子导航到另一个标签时,传递一个 Extra 和 Intent
MainTabActivity parent = (MainTabActivity)getParent();
parent.getStoredTabIntent().putExtra(AnActivity.START_VIEW, AnActivity.PAGE_TWO);
//Navigate to the tab
parent.getTabHost().setCurrentTabByTag(AnActivity.TAB_GALLERY);
然后,在 AnActivity 的 onCreate
Bundle extras = getIntent().getExtras();
if(extras != null && extras.containsKey(START_VIEW)){
switch (extras.getInt(START_VIEW)) {
case PAGE_TWO:
doSomething();
break;
default:
break;
}
//Erase the Extra so that navigating to this Tab always displays the standard view unless specified otherwise
MainTabActivity parent = (MainTabActivity)getParent();
parent.getStoredTabIntent().putExtra(AnActivity.START_VIEW, "");
}else{
doStandardStuff();
}
应用程序解决方案也很好,但我认为我不需要它来做其他任何事情,因此我宁愿坚持使用活动。