4

There are 4 Tabs in a TabHost, let them be A, B, C, and D. Now each one is just an index page and clicking on any of them shows a different activity.

The problem is that I need to start another activity when the user selects something from the content displayed in the tab. The other activity should also be displayed in the parent tab itself. Is it possible? Or will I have to try something else?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
JaVadid
  • 7,107
  • 13
  • 42
  • 54

5 Answers5

5

Try this, found this solution in android cookbook, http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1693&recipeFrom=ViewTOC

Rukmal Dias
  • 3,242
  • 1
  • 34
  • 28
  • While this may theoretically answer the question it would be preferable for you to edit the answer to include the essential parts of the solution, and provide the link for reference. – Peter O. Feb 06 '12 at 11:40
2

Can't you change the contentView of your tab instead of starting a new Activity ?

Maybe I'm wrong but I think also that starting an activity in a tab isn't possible because the TabView is hosted in a activity and not the opposite (Tabview don't host an activity per Tab).

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • is there anyway i can erase all the contents and replace them with another layout... it would be helpful if u could give some resource or simple coding... – JaVadid Dec 31 '09 at 10:29
  • guess so... so it seems it isnt possible... actually i need to save the state of the previous activities too... – JaVadid Dec 31 '09 at 10:31
  • I'm not familiar with tabs, try looking the TabHost javadoc (http://developer.android.com/reference/android/widget/TabHost.html). – tbruyelle Dec 31 '09 at 11:24
  • Well Thomas at last it worked for me by jus changing the ContentView to the new Content i wish to show and processing the data through a common universal Listener... Though it needed very complex bit of coding... and as a matter of fact i haven't completed the coding as i write this... But m sure this is the best method the Tabs can offer... – JaVadid Jan 02 '10 at 13:07
  • Happy to read that, more than checking my answer you can also up it to say it was usefull :) – tbruyelle Jan 02 '10 at 23:58
  • 1
    Activities can really be hosted as tabs (and this is a default in Google's tutorial): http://developer.android.com/resources/tutorials/views/hello-tabwidget.html – Sergii Rudchenko Aug 19 '11 at 08:43
1

I think the common consensus is that it is best not to use individual Activities as tab content due to these limitations. See these questions and answers for pointers to alternatives:

Android: Why shouldn't I use activities inside tabs? Android - Tabs, MapView, activities within tabs

Community
  • 1
  • 1
Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
1

To summarize the link that Rukmal Dias provided. Here's what you do:

  1. Change your current Activity (that's in a tab) to derive from ActivityGroup
  2. Create a new intent for the Activity you want to switch to
  3. Copy/Paste and call this function in your current activity where "id" is the "android:id" for the layout of the new activity you want to switch to

     public void replaceContentView(String id, Intent newIntent){
          View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); 
          this.setContentView(view);}
    

Here's an example of how I make the call to switch views from my current Tabbed Activity:

public void switchToNextActivity(View view)
{
    Intent myIntent = new Intent(getApplicationContext(), MyNextActivity.class);
    replaceContentView("next_activity", myIntent);
}
Jonesy
  • 121
  • 1
  • 4
0

It looses the view hierarchy. When you press the back button, in my case, the app closes.

Carlos
  • 1