我有一个TabActivity
,其中包含3个tabs
。我将currentActivity
设置为1
而不是默认值0
。当我启动TabActivity
时,我发现Activity0也运行但Tabactivity显示了我设置为currentActivity
的Activity1的界面。此外,当我单击tab2(包括Activity2)并再次单击tba1或tab0时,它们不再运行。我认为TabActivity只允许运行一次。每当我点击标签时,我想在标签中重新启动每个活动。我想保留这些内容,以便在点击后退按钮时它应该是可追踪的。
public class ActivityInTab extends TabActivity implements OnTabChangeListener{
TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityintab);
tabHost = getTabHost();
TabSpec meSpec = tabHost.newTabSpec("Ben");
meSpec.setIndicator("Ben", getResources().getDrawable(R.drawable.icon_me));
Intent meIntent = new Intent(this, MeActivity.class);
meSpec.setContent(meIntent);
TabSpec searchSpec = tabHost.newTabSpec("Arama");
searchSpec.setIndicator("Çevrende Ara", getResources().getDrawable(R.drawable.icon_search));
// Intent searchIntent = new Intent(this, SearchActivity.class);
// searchSpec.setContent(searchIntent);
TabSpec resultSpec = tabHost.newTabSpec("Aradıklarım");
resultSpec.setIndicator("Arama Sonuçlarım", getResources().getDrawable(R.drawable.icon_result));
// Intent resultIntent = new Intent(this, ResultActivity.class);
// resultSpec.setContent(resultIntent);
tabHost.addTab(searchSpec);
tabHost.addTab(meSpec);
tabHost.addTab(resultSpec);
tabHost.setCurrentTab(1);
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("Ben") && tabHost.getCurrentTab()==0){
MeActivity meact = (MeActivity) getLocalActivityManager().getActivity(tabId);
}
if(tabId.equals("Arama") && tabHost.getCurrentTab()==1){
SearchActivity sact= (SearchActivity) getLocalActivityManager().getActivity(tabId);
}
if(tabId.equals("Aradıklarım") && tabHost.getCurrentTab()==2){
ResultActivity ract = (ResultActivity) getLocalActivityManager().getActivity(tabId);
}
}
}
答案 0 :(得分:2)
为标签意图设置标记Intent.FLAG_ACTIVITY_CLEAR_TOP
。
Intent meIntent = new Intent(this, MeActivity.class);
meIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
还有像这样的评论标签
Intent searchIntent = new Intent(this, SearchActivity.class);
searchIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);