开发者

android first tab intent oncreate always called regardless we set tab2 as default tab

开发者 https://www.devze.com 2022-12-21 23:17 出处:网络
Following is the example of tabs with intent data. While debugging i found that always when first tab we add in tab host in our case following tab

Following is the example of tabs with intent data.

While debugging i found that always when first tab we add in tab host in our case following tab

tabHost.addTab(tabHost.newTabSpec("tab1")
                    .setIndicator("list")
                    开发者_JAVA技巧.setContent(new Intent(this, List1.class)));

oncreate method of "List1" intent get called regardless it is our current tab or not even if if i define tab2 as a current tab how to fix this ?

public class Tabs3 extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TabHost tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("list")
                .setContent(new Intent(this, List1.class)));

        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("photo list")
                .setContent(new Intent(this, List8.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        // This tab sets the intent flag so that it is recreated each time
        // the tab is clicked.
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("destroy")
                .setContent(new Intent(this, Controls2.class)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
    }
}


setDefaultTab(1);

seems not to be working in TabActivity when separate Activities are used as Tab Content.

Use following instead of this method,

tabHost.setCurrentTab(1);

This will set "photo list" (i.e second tab) as the selected or default tab...


I have found this same behavior as well, and I do not have a specific fix. But I do know of a work-around.

Instead of attaching Activities to each tab, attach a View to each tab. You can then handle the data passing very easily as each view will be in the same Activity. This also eliminates the need to pass information using Intents. Furthermore, you can create (or inflate) your Views as you need them and with more control.

Good luck, -scott

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号