Good day, I have three activities with their corresponding fragments A, B and C. Fragment A is a static fragment the others fragments are dynamic.
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.
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. I have this simple code in the activity B onCreate method:
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? 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. Any thoughts?
You can’t implement the listener just in the
Bactivity because theAactivity has to be able to show the fragmentCin landscape. If you are worrying about code duplication then you could implement a base activity for which you implement the callback to show fragmentCand make your two activities extend from that base class.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
Bactivity showing fragmentB) and by clicking(or whatever you do) you replace the fragment withCthis will not be carried to activityAif you switch orientation to landscape.