Activity in TabActivity doesn't run onCreate Method when clicked second time

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 04:01:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!