简体   繁体   中英

android tabs - starting a new activity

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?

试试这个,在android cookbook中找到这个解决方案, http://androidcookbook.com/Recipe.seam ; jsessionid = 5424397F3130CE7769FF47DD67742911 recipeId = 1693&compaipeFrom = ViewTOC

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).

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

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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM