简体   繁体   English

从活动中启动片段

[英]Launching Fragment from activity

In my MainActivity , I have a viewPager containing two containers "A and B". 在我的MainActivity中 ,我有一个viewPager,其中包含两个容器“ A和B”。 Both containers are possessed with their own Fragments. 这两个容器都拥有自己的片段。

In container B; 在容器B中; I again have a viewPager containing multiple fragments.( For example one,two,three,four ) 我再次有一个包含多个片段的viewPager 。( 例如,一,二,三,四

On launch of my app MainActivity gets load with container A's homeFragment. 在启动我的应用程序时,MainActivity将加载容器A的homeFragment。

What I need to do is,For a given condition I have to launch fragment two of container B from onResume of my MainActivity. 我需要做的是,对于给定条件,我必须从MainActivity的onResume启动容器B的第二个片段

What I've able to achieve is , I have successfully redirect to container B's fragment one but couldn't able to redirect towards fragment two from onResume of MainActivity, any help will be appreciate. 我能实现的是,我已经成功重定向到容器B的 片段1,但无法从MainActivity的onResume重定向到片段2 ,将不胜感激。

如果要使用Intent重定向到片段B,则传递子片段的int位置,然后在片段B侧检查是否有其他意图,并设置当前页面,该页面是通过intent.getIntExtra()收到的。

如果我理解清楚,您想在打开它时显示它的第二页,如果可以,可以使用mViewPager.setCurrentItem(1, true);

我认为您想设置特定页面..为此,您可以将viewpager设置为:

viewpager.setCurrentItem(item, true);

Certainly,I found a way of achieving this by using static concept. 当然,我找到了一种使用静态概念实现此目标的方法。 I have declared a static variable inside container B's fragment two,and set its value in onResume of MainActivity. 我在容器B的片段2中声明了一个静态变量,并在MainActivity的onResume中设置其值。

When onResume calls with given condition it sets static variables value to the required fragment, and in my container B,I have used this value to set setCurrentItem() of B'viewPager. 当在给定条件下进行onResume调用时,它将静态变量值设置为所需的片段,并且在我的容器B中,我已使用该值设置B'viewPager的setCurrentItem()。

ps: I know it quite confusing as it seems but not too complex in practice,work fine. ps:我知道它看起来很混乱,但实际上并不太复杂,可以正常工作。

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

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