简体   繁体   English

android标签 - 开始一项新活动

[英]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. TabHost中有4个选项卡,让它们分别为A,B,C和D.现在每个选项卡只是一个索引页面,单击其中任何一个都会显示不同的活动。

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 ? 你不能改变你的标签的contentView而不是开始新的活动吗?

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). 也许我错了,但我认为在Tab中启动一个活动是不可能的,因为TabView是在一个活动中托管而不是相反(Tabview不为每个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:为什么我不应该在标签内使用活动? Android - Tabs, MapView, activities within tabs Android - 标签,MapView,标签内的活动

To summarize the link that Rukmal Dias provided. 总结一下Rukmal Dias提供的链接。 Here's what you do: 这是你做的:

  1. Change your current Activity (that's in a tab) to derive from ActivityGroup 将当前活动(在选项卡中)更改为从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 复制/粘贴并在当前活动中调用此函数,其中“id”是“android:id”,用于要切换到的新活动的布局

      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. 按下后退按钮,在我的情况下,应用程序关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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