How to use the same Activity for 3 different tabs in TabActivity with different inputs

1.3k views Asked by At

I have an TabActivity which has 3 Tabs reusing the same Activity for all the 3 tabs.

I am differentiating by means of extras(intent.putextras()) which i shall display the contents of each tab

Now the problem is when we switch the tab from each other the extra values which i passed through Intent is coming wrong for the second tab.

As i understood when we created the FirstTab, the only Activity is created and so the contents are drawn as per it.

when we add the secondTab the intent storing the extra value

and again when we creating the third tab the Intent over-writing the extra value.

so when ever we are switching to 2nd tab, i'm receiving the 3rd Tab extra value and hence showing wrong contents to the user.

here is the code,

public class ContentsTab extends TabActivity implements OnTabChangeListener{

public static final String _ID = "_id";
public static final int ID_1 = 1;
public static final int ID_2 = 2;
public static final int ID_3 = 4;

private TabHost mTabHost;
private static Intent newIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);     

newIntent = new Intent(this, ShowContents.class);

mTabHost = getTabHost();

    newIntent.putExtra(_ID, ID_1);
mTabHost.addTab(mTabHost.newTabSpec(INT_EXT_MEM)
            .setContent(newIntent)
            .setIndicator("one"));

    newIntent.putExtra(_ID, ID_2);
    mTabHost.addTab(mTabHost.newTabSpec(EXT_MEM)                
            .setIndicator("two")
            .setContent(newIntent));

    newIntent.putExtra(_ID, ID_3);
    mTabHost.addTab(mTabHost.newTabSpec(INT_MEM)
            .setContent(newIntent)
            .setIndicator("three"));

    mTabHost.setCurrentTabByTag("one");

    mTabHost.setOnTabChangedListener(this);
}

@Override
public void onTabChanged(String tabId) {
    //This is only coming once the Tab is changed
    Log.d(TAG, "onTabChanged arg0 = "+tabId);               
 }
 }

showcontents.class

public class ShowContents extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);     

    Bundle extras = getIntent().getExtras();

    int mId_ContentType;

    mId_ContentType = extras != null? extras.getInt(_ID): -1;

    updateData(mId_ContentType);

  }
 }

Here the illustration, when user presses tab1, ShowContents.class should receive 1 as extras and same with tab2 and tab3, but the following values are received Tab1 - 1 Tab2 - 4(correct value should be 2) Tab3 - 4

Tab1 and Tab3 values are coming correct.

Is there any alternative to send the correct extra parameters to Tab2? using the same class Activity?

Please help me

Thanks for your time :)

1

There are 1 answers

0
bala On

You need to have 3 different instances of the activity. You can do something like this

newIntent1 = new Intent(this, ShowContents.class);
newIntent2 = new Intent(this, ShowContents.class);
newIntent3 = new Intent(this, ShowContents.class);

Then use these 3 instances for 3 tabs. Should be fine like this.