4

I use a TabHost. The below code to call AActivity.

intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);

And it is in the tab. But in AActivity I call BActivity. The BActivity will open new page, but not in the tab. How to let it on the tab frame? AActivity use below code to call BActivity:

it = new Intent(this, BActivity.class);
startActivity(it);
brian
  • 6,802
  • 29
  • 83
  • 124

3 Answers3

5

If you want to open multiple activity in Tab then on Place of activity use Activity group for par tab and switch view in this activity group for open multiple Activity in single tab

you can take some help from this tutorial

loks
  • 490
  • 3
  • 10
2

You need to change the current selected tab. There's a method called setCurrentTabByTag(String tag) in the TabHost class that will do that, just pass the tag name of your tab (which in your case is B), or you can use the setCurrentTab(int index) and pass the tab index.

Example. Usually I have a MainActivity class, which is my TabActivity. Inside of this class, I will create all tabs that I need on the onCreate method.

First I set some static int with the tabs indexes.

    // Tab index.
    public static int FIRST_TAB = 0;
    public static int SECOND_TAB = 1;

Later, I create my tabs in the onCreate method.

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // Setting the content view.
     setContentView(R.layout.main);
     // Getting the TabHost object.
     mTabHost = getTabHost();
     // Declaring the Intent object for the tabs.
     Intent intent;
     // Creating the First tab.
     intent = new Intent().setClass(this, MyFirstActivity.class);
     addTab(mTabHost, "First", FIRST_TAB, intent)
     // Creating the Second tab.
     intent = new Intent().setClass(this, MySecondActivity.class);
     addTab(mTabHost, "Second", SECOND_TAB, intent);
     // Setting the current tab.
     switchTab(FIRST_TAB);
}

public void addTab(TabHost host, String title, String tag, Intent intent) {
     TabHost.TabSpec spec = host.newTabSpec(tag);
     spec.setContent(intent);
     spec.setIndicator(title);
     host.addTab(spec);
}

And last the method that will change the current tab.

public void switchTab(int index) {
    mTabHost.setCurrentTab(index);
}

Later, inside of the MyFirstActivity you can call the MainActivity swichTab method and pass the index of the tab to change it. You can retrieve the MainActivity calling the getParent() method of the Activity class.

MainActivity parent = (MainActivity)getParent();
Mokkun
  • 1,796
  • 1
  • 23
  • 32
1

In the tab activity class where the tabhost is created, implement the following method.

public void switchTab(int tab){
            tabHost.setCurrentTab(tab);
}

In AActivity/BActivity implement the following method and call it on any event(that you need):

public void switchTabInActivity(long indexTabToSwitchTo){
            TabActivity tabActivity;
            tabActivity = (TabActivity) this.getParent();
            tabActivity.switchTab(indexTabToSwitchTo);
}

Here TabActivity is the class where tabhost is created

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
Walid Hossain
  • 2,724
  • 2
  • 28
  • 39
  • This mean switch tab? But BActivity is not a tab, it just another new activity. – brian Jan 02 '12 at 03:28
  • Then you need to use ActivityGroup. Here is an example http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity – Walid Hossain Jan 02 '12 at 03:31
  • Another example http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html – Walid Hossain Jan 02 '12 at 03:35
  • Don't think that using a ActivityGroup is a good choice. Take a look at Mark Murphy's comment: http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity#comment-295 – Mokkun Jan 02 '12 at 03:57
  • But I'm not sure that ViewFlipper will cache ur current activity status & when you are back from 2nd activity to 1st activity it shows a new 1st Activity or cached 1st activity. But using activity group you'll get the previous state of 1st Activity not new 1st Activity. – Walid Hossain Jan 02 '12 at 04:07
  • @WalidHossain With a ViewFlipper you won't work with an Activity, you'll work just with views. That can work depending on your needs. The ActivityGroup is deprecated, but you can use fragments instead: http://stackoverflow.com/questions/7579721/activitygroup-is-deprecated. Never tried it though. – Mokkun Jan 02 '12 at 08:03