[英]Multiple fragments and activities interaction design
Good day, I have three activities with their corresponding fragments A
, B
and C
. 美好的一天,我进行了三个活动,并带有相应的片段
A
, B
和C
Fragment A
is a static fragment the others fragments are dynamic. 片段
A
是静态片段,其他片段是动态片段。
Activity A
is already implementing a listener for fragment A
, which is used to load fragment B
in landscape orientation or move to new screen in single pane. 活动
A
已经实现了片段A
的侦听器,该侦听器用于横向加载片段B
或移动到单个窗格中的新屏幕。
What i want is when a certain button is pressed in fragment B
which calls up fragment C
, I want it to be replaced by fragment C
in landscape orientation(fragment A
will still be present) or bring up a new screen in single pane mode. 我想要的是在片段
B
中按下某个按钮时调用片段C
,我希望它在横向上被片段C
代替(片段A
仍然存在)或以单窗格模式显示新屏幕。 I have this simple code in the activity B onCreate
method: 我在活动B的
onCreate
方法中有以下简单代码:
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
I am trying to avoid fragment to fragment communication since it is frowned at. 我正试图避免零散的沟通,因为它被皱眉了。 So does this mean I have to implement listeners for fragment
B
in both activity A
and B
, am guessing that when in landscape orientation activity A
would load up fragment C
and in portrait, B
takes over? 那么,这是否意味着我必须首先落实片段听众
B
两个活动A
和B
,猜的是,当横向活动A
会加载片段C
和肖像, B
接管? Is there a better way?. 有没有更好的办法?。 I thought of only implementing the listener in activity
B
and passing to activity A
when in landscape orientation but I think it would have been finished before it even got to pass due to the above code. 我考虑过只在活动
B
实现侦听器,然后在横向放置时传递给活动A
,但由于上述代码,我认为它应该在它通过之前就已经完成。 Any thoughts? 有什么想法吗?
...so does that mean i implement listener for fragment B in both Activity A and B, am guessing that when in Landscape Orientation Activity A would load up Fragment C and in portrait, B takes over or is there a better way?
...所以这是否意味着我在活动A和活动B中都实现了片段B的侦听器,所以我猜想,当以横向方向活动时,活动A会加载片段C,而在人像中,活动B将接管片段B还是有更好的方法? I thought of only implementing the listener in Activity B and passing to Activity A when in Landscape Orientation but i think it would have been finished before it even got to pass due to the above code.
我考虑过仅在活动B中实现监听器,而在横向取向时传递给活动A,但由于上述代码,我认为它应该在它通过之前就已经完成。
You can't implement the listener just in the B
activity because the A
activity has to be able to show the fragment C
in landscape. 您不能仅在
B
活动中实现侦听器,因为A
活动必须能够在横向显示片段C
If you are worrying about code duplication then you could implement a base activity for which you implement the callback to show fragment C
and make your two activities extend from that base class. 如果您担心代码重复,则可以实现一个基本活动,为此您可以实现回调以显示片段
C
并使两个活动从该基类扩展。
As you didn't post any real code on how you manage those fragments, keep in mind that if you are in the portrait orientation(in the B
activity showing fragment B
) and by clicking(or whatever you do) you replace the fragment with C
this will not be carried to activity A
if you switch orientation to landscape. 由于您没有发布任何有关如何管理这些片段的真实代码,因此请记住,如果您是纵向(在显示片段
B
的B
活动中),然后单击(或执行任何操作),则将片段替换为如果您将方向切换为横向,则C
不会被带到活动A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.