TabActivity that create an intent onClick button but the tabhost of activity receiver disappear

﹥>﹥吖頭↗ 提交于 2020-01-06 13:52:51

问题


I have 2 activity: A, B. The first is an TabActivity and the second an Activity. Inside A i have a clickable button that make an intent to call the other activity.

public class A extends TabActivity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("a").setContent(R.id.a).setIndicator("a"));

        b = new Intent(this,B.class);
    tabHost.addTab(tabHost.newTabSpec("b").setContent(b)
            .setIndicator(this.getString(R.string.b)));

        btn.setOnClickListener(this);
    }


    public void onClick(View arg0) {
        if (arg0 == btn) {
        startActivity(b);
        }
    }
}

Inside B, i have a thread that gets data from internet every time that the button in A class is clicked:

public class B extends Activity {
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.search_result);

       searchHandler = new Handler() {
           @Override
           public void handleMessage(Message msg) {
               draw(msg.obj.toString());
           }
    }

    Intent intent = getIntent();
    if(..)
        sendHttpRequest();
}

Data returns correctly and I can manage it, but my tabhost disappear. How I can solve?


回答1:


The android way to automatically change tab is:

tabHost.setCurrentTab(tab);

and not using:

startActivity(intent);

To use it on all your Activity the best way is to set your TabHost public and static.




回答2:


whenever you click a button you call an activity which is not child activity of tab activity. so tabhost is disapear. call activity b in tab activity just like this

 tabHost.addTab(tabHost.newTabSpec("Tab")
                    .setIndicator("Tab")
                    .setContent(new Intent(this, b.class)
                   ));


来源:https://stackoverflow.com/questions/10437181/tabactivity-that-create-an-intent-onclick-button-but-the-tabhost-of-activity-rec

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