tags:

views:

91

answers:

2

Hi, I want Access a single Activity using 2 different tabs.For Ex I have a single Activity like People and two tabs those names are tab1 and tab2.when i click on tab1 I want to display the people page as my group and when i click on tab2 that same page displayed the show all title.That means I want to Access a single Intent for two tabs In the same way the Information in that Activity is displayed According to Tab.For this purpose what can i do?Give me some suggestions.Thanks in advance.

+1  A: 

Do not put activities as the contents of your tabs. Put views as the contents of your tabs. Here is a sample project demonstrating this technique.

CommonsWare
why would you say that in such black and white terms? I use activities within my tabs and actually prefer it. Conceptually, what I put in my tabs is what I would put in an "Activity", and there is a mechanism to make that happen easily so I have access to Activity life cycle methods at the tab host level as well as the tab level (ie. onResume when that Activity's tab is selected). If this is not recommended, why would there be a TabSpec.setContent(Intent) method signature in the framework?
Rich
"why would you say that in such black and white terms?" First, it wastes heap memory. Second, it wastes CPU time. Third, it wastes stack space. Fourth, developers tend to get confused when trying to use it, which increases the number of support questions. Fifth, I have yet to see how it adds anything -- whatever code organization you get from it can be achieved perfectly well with another class structure, not an `Activity`. As to why it is in the framework, there's lots of things in the API that aren't optimal. I do apologize for the harsh tone of my answer, though.
CommonsWare
+1  A: 

I don't think I would reuse the same activity for different tabs. Is there a reason you can't use separate activities to represent different views of the same data? The reason I say that is that you would have to write code inside the activity to differentiate between which tab was selected, and I think your code base would be much cleaner if that was inherent in your classes.

I have code in my TabActivity similar to the following for setting activities as the content of my tabs via intents:

    mTabHost = getTabHost();

    // Tab1
    Intent tab1Intent = new Intent(this, ActivityForTab1.class);
    mTabHost.addTab(mTabHost.newTabSpec(TabTag1).setIndicator(TabLabel1).setContent(tab1Intent));     

    // Tab2
    Intent tab2Intent = new Intent(this, ActivityForTab2.class);
    mTabHost.addTab(mTabHost.newTabSpec(TabTag2).setIndicator(TabLabel2).setContent(tab2Intent));     

    mTabHost.setCurrentTab(0);
Rich