问题
I have a TabActivity
which includes 3 tabs
. And I set currentActivity
as 1
not the default one, 0
. When I start my TabActivity
I figured out that Activity0 also runs but Tabactivity shows the interface of Activity1 which I set as currentActivity
. Furthermore when I click tab2 (include Activity2) and click again tba1 or tab0, they dont run again. As I think TabActivity just let to run one time. I would like to restart each activiy in tab whenever I click tabs. And I would like to keep these in trace so that when clicking back button it should be tracable.
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);
}
}
}
回答1:
set flag Intent.FLAG_ACTIVITY_CLEAR_TOP
for your tab intents.
Intent meIntent = new Intent(this, MeActivity.class);
meIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
And also for the commented tabs like this
Intent searchIntent = new Intent(this, SearchActivity.class);
searchIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
来源:https://stackoverflow.com/questions/15967384/activity-in-tabactivity-doesnt-run-oncreate-method-when-clicked-second-time