简体   繁体   中英

How call an activity to tabactivity from child

i made an application with a tab activity like this:

intent = new Intent().setClass(this, FirstActivity.class);  
spec  = tabHost.newTabSpec("Tab1").setIndicator("Tab1",res.getDrawable(R.drawable.tab1)).setContent(intent); 
tabHost.addTab(spec); 

In FirstActivity.class there is a button with onclick listener:

@Override
public void onClick(View v) {

      Intent i = new Intent(FirstActivity.this, OtherActivity.class);
      i.putExtras(bundle);
      startActivity(i);

}

But when i click, "OtherActivity" hide tab. I'm looking for a way to click on button and "OtherActivity" open in TabActivity instead of "FirstActivity".

You can't use startActivty method to switch OtherActivity, you must add FirstActivity and OtherActivity in the tabhost, use tabhost to switch activity. In FirstActivity, you can send a broadcast then in TabActivity register the receiver and receive the click action message, use tabhost switch.

In android new version, you can use fragment instead Activity to show mul tabs.

You need to create a intermediate tab Activity like following

public class IntermediateActivity extends TabGroupActivity {

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    // TODO Auto-generated method stub
                    super.onCreate(savedInstanceState);
                    startChildActivity("IntermediateActivity", new Intent(this,OtherActivity.class));
                }

}

In Your Button click write following code

 Intent i = new Intent(getParent(),IntermediateActivity.class); 
 TabGroupActivity parentActivity = (TabGroupActivity) getParent();
 parentActivity.startChildActivity("move to otheractivity activity", i);                                                                                                                                                                                                                                              

Hope this helps..

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