androidandroid-intentandroid-tabhosttabactivity

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


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?


Solution

  • 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.