Passing fragment's B saved state to Activity A


#1

After reading chapter 14, I decided to try something like this:

I have two activities and two fragments (each fragment corresponding to one activity). Activity A (in portrait mode) only displays fragment A. Activity B (in portrait mode) only displays fragment B.

My problem appears when Activity A (MainActivity) in landscape has to display fragment A (that belongs to itself) and fragment B (that belongs to activity B). I can not seem to be able to save on to activity A (MainActivity) the state of fragment B.

This is what I’ve tried in fragment B:

... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstanceState(); } ...

Can I use this code to save the state of fragment B (all its local variables)?

In activity A (MainActivity) I tried to detect if the device is on landscape:

[code]if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// restore fragment’s B state so the recent data passed into it shows up
// in landscape (in Activity A).
}

else {
// keep doing what you’re doing in portrait view.
}[/code]

Every time I rotate my device, fragment B comes back empty, even though in portrait mode I have data displaying (data passed from selecting an item from a list in fragment A).

How do I capture the state of fragment B in this particular case? Thanks much.