Jump to the specified Tab page in TabHost

Source: Internet
Author: User

Recently I encountered a problem when using TabHost:
Four activities are added to TabHost as tab pages. From left to right, they are called tab1, tab2, tab3, and tab4. However, no matter which Activity I click to jump to when entering the TabHost page, the tab1 Activity will always be executed first. However, the expected result is that I only want to execute the Activity of tab2.
Analysis: I checked the source code of TabHost 2.1 and found the addTab method, as shown below.
/**
* Add a tab.
* @ Param tabSpec Specifies how to create the indicator and content.
*/
Public void addTab (TabSpec tabSpec ){

If (tabSpec. mIndicatorStrategy = null ){
Throw new IllegalArgumentException ("you must specify a way to create the tab indicator .");
}

If (tabSpec. mContentStrategy = null ){
Throw new IllegalArgumentException ("you must specify a way to create the tab content ");
}
View tabIndicator = tabSpec. mIndicatorStrategy. createIndicatorView ();
TabIndicator. setOnKeyListener (mTabKeyListener );

// If this is a custom view, then do not draw the bottom strips
// The tab indicators.
If (tabSpec. mIndicatorStrategy instanceof ViewIndicatorStrategy ){
MTabWidget. setDrawBottomStrips (false );
}
MTabWidget. addView (tabIndicator );
MTabSpecs. add (tabSpec );

If (mCurrentTab =-1 ){
SetCurrentTab (0 );
}
}
Focus on the last two pieces of code. When the variable mCurrentTab is equal to-1, setCurrentTab (0); then find the mCurrentTab variable and find its declaration as follows:
Protected int mCurrentTab =-1;
Based on the above situation, I guess it is because of the value assignment of the variable mCurrentTab, so when the addTab method is executed, setCurrentTab (0) is executed; method, in this way, the first Activity will be executed first. The Activity added by calling addTab for the first time will always be executed.
Solution:
Based on the above situation, the reflection mechanism is used to control the assignment of the variable mCurrentTab of TabHost, so that independent access to the Activity can be realized. There are two steps.
Step 1: Change the mCurrentTab value to non-1. The Code should be written before the addTab method is called. This prevents the last two statements of the addTab method from being executed. As follows:
Try
{
Field idcurrent = tabHost. getClass ()
. GetDeclaredField ("mCurrentTab ");
Idcurrent. setAccessible (true );
Idcurrent. setInt (tabHost,-2 );
}
Catch (Exception e)
{
E. printStackTrace ();
}
Step 2: Modify the value of mCurrentTab after the addTab method is executed. This is to be executed normally when the setCurrentTab method is called, as shown below:
Try
{
Field idcurrent = tabHost. getClass ()
. GetDeclaredField ("mCurrentTab ");
Idcurrent. setAccessible (true );
If (tadid = 0)
{
Idcurrent. setInt (tabHost, 1 );
}
Else
{
Idcurrent. setInt (tabHost, 0 );
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
Finally, write the overall functional Code as follows:
// Obtain the Id of the tab to jump
Bundle extras = getIntent (). getExtras ();
Resources resources = getResources ();
String defaultTab = extras. getString (STARTING_TAB );
Int tadid = defaultTab = null? 2: Integer. valueOf (defaultTab );
// Set mCurrentTab to non-1. setCurrentTab () is not entered during addtab ()
Try
{
Field idcurrent = tabHost. getClass ()
. GetDeclaredField ("mCurrentTab ");
Idcurrent. setAccessible (true );
Idcurrent. setInt (tabHost,-2 );
}
Catch (Exception e)
{
E. printStackTrace ();
}

Intent Intent1 = new Intent (this, Activity1.class );
Intent1.putExtras (extras );
TabHost. addTab (tabHost. newTabSpec (Intent1_TAB). setIndicator (
Resources. getString (R. string. Intent1). setContent (
Intent1 ));

Intent Intent12 = new Intent (this, Activity2.class );
Intent12.putExtras (extras );
TabHost. addTab (tabHost. newTabSpec (Intent12_TAB). setIndicator (
Resources. getString (R. string. Intent12). setContent (
Intent12 ));

Intent Intent13 = new Intent (this, Activity3.class );
Intent13.putExtras (extras );
TabHost. addTab (tabHost. newTabSpec (Intent13_TAB). setIndicator (
Resources. getString (R. string. Intent13). setContent (
Intent13 ));
// Set the mCurrentTab to be different from the tadid, And the array cannot be out of bounds (0-2). Ensure that the setCurrentTab () method that first enters the tab runs normally.
Try
{
Field idcurrent = tabHost. getClass ()
. GetDeclaredField ("mCurrentTab ");
Idcurrent. setAccessible (true );
If (tadid = 0)
{
Idcurrent. setInt (tabHost, 1 );
}
Else
{
Idcurrent. setInt (tabHost, 0 );
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
// Enter the incoming Tab
TabHost. setCurrentTab (tadid );

 

From heart fly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.