TabHost Activity

I am using TabHost. The code below is for calling 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 on the tab. But in AActivity, I call BActivity. BActivity will open a new page, but not on a tab. How to do this on the tab frame? ACTIVITY use the code below to call BActivity:

 it = new Intent(this, BActivity.class); startActivity(it); 
+4
source share
3 answers

If you want to open several actions in the Tab, and then in the place of action, use the group of actions for the general tab and the switch in this group of actions to open several actions in one tab

you can get help this tutorial

+5
source

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

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

First, I set some static int with tab indices.

  // 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 the last method that will change the current tab.

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

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

 MainActivity parent = (MainActivity)getParent(); 
+2
source

In the activity class of the tab where the taboo 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 (which you need):

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

Here TabActivity is the class in which tabhost is created

+1
source

Source: https://habr.com/ru/post/1388874/


All Articles