How to go to a specific tab from a different class?

北城余情 提交于 2019-12-24 13:22:34

问题


I have a TabHost Activity with three tabs. On the second tab I have a button, clicking which would open a new Activity class (not part of the TabHost). On clicking a button in the new Activity class, it should return to the Tab 2 of the TabHost class but it returns to Tab 1. How should I fix it?

Here is the code in my new Activity class:

ImageButton btn1 = (ImageButton)findViewById(R.id.close);
btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(ES_pic2.this, Work.class);
            startActivity(intent);
            ES_pic2.this.finish();  
        }           
    });

The code of my TabHost class:

private void tabs(){

    TabHost tabs=(TabHost)findViewById(R.id.tabhost);

    tabs.setup();

    TabHost.TabSpec spec=tabs.newTabSpec("tag1");

    spec.setContent(R.id.tab1);
    spec.setIndicator("WORK 1");
    tabs.addTab(spec);

    spec=tabs.newTabSpec("tag2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("WORK 2");
    tabs.addTab(spec);  

    spec=tabs.newTabSpec("tag3");
    spec.setContent(R.id.tab3);
    spec.setIndicator("WORK 3");
    tabs.addTab(spec);

}

I did have look at other questions with similar problems, but they didn't help me.


回答1:


In the onResume of the activity in which tabs are, use setCurentTab function to set the current tab. If you want to set the tab to the same index which was selected before you started the new activity, then you can save the index in SharedPreference or something. Then use that to set the current tab.



来源:https://stackoverflow.com/questions/18539664/how-to-go-to-a-specific-tab-from-a-different-class

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