简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM