0

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?

user
  • 86,916
  • 18
  • 197
  • 190
irobotxx
  • 5,963
  • 11
  • 62
  • 91

1 Answers1

1

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

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

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.

user
  • 86,916
  • 18
  • 197
  • 190
  • thanks for the response. am fine with implementing the listener in both of them. i just had concern if i was in portrait orientation whether both activities will call up C. As for your 2nd point, not really thought of that. maybe creating a static method in Activity A which Activity B can call up when switching to orientation. what would you suggest?.. – irobotxx Jul 28 '12 at 20:26
  • my code for managing this fragments(except am using ActionBarSherlock)is like how it is on the android developer resource and other tutorials out there so did not think it was necessary bringing it up again. :) – irobotxx Jul 28 '12 at 20:29
  • @manuelJ Calling a method in activity `A` isn't a recommended approach in android as you don't have control over the activity life(it could be destroyed while you are in the `B` activity). I've made a simple example on how I would approach your current situation(find it here https://gist.github.com/3197234). I hope you understand it(it worked, but I haven't tested it too much), it's a single activity with two different layouts and some extra stuff to make the back button to work with the fragments. – user Jul 29 '12 at 10:16
  • Thanks a lot!! just what i needed to point me in the right direction. – irobotxx Jul 29 '12 at 17:18