Tip:
Highlight text to annotate it
X
This video explores fragments in Android. It's the 4th part of a 7 part series.
In thus video we introduce dynamic fragments.
The objectives are stated here with a timeline for when they appear in the video
OK, so if you're watching these videos in order
([laughs] the order I intended you to),
we just finished up with a demo of StaticFragmentSingleActivity.
Now let's just take a quick review of what we did there
and talk about how what we're going to do now is going to be just slightly different.
In a layout_large folder,
we defined our user interface for when we're going to be running on a tablet.
What we there was we set an ID to the LinearLayout to "root_layout_large"
and then we statically put in two Fragments,
meaning both Fragments are always there.
Then we made an identical layout file in our default folder,
which we generally think of as being for a phone where there's less real estate.
And we set an ID of "root_layout_default."
Other than that, those two layout files were identical except for the ID,
and we always statically defined two Fragments,
which meant they always lived in memory.
So if we just look at this portion of our View tree, starting from this LinearLayout,
we know that we have a LinearLayout
and that we created two Fragments
(our listFrag and our detailFrag),
what I was calling Fragment A, the listFrag, and Fragment B, the detailFrag, amber and blue.
And of course when we're running on a tablet,
(which we knew because of the ID that we would grab from LinearLayout based on the layout_large folder),
we always had both Fragments in what was called the "show" mode, or you can think of it is being "visible"
as our methods were called "show" and "hide."
And so then when we were working with a phone
we still always had A (amber listFrag) and B (blue detailFrag) in the View tree.
(They always existed, it's just that we sometimes invoked
show and hide methods so that we only saw one at a time.)
But the important thing to understand is, both of these Fragments always existed in the View tree under both scenarios,
tablet and phone,
its just that when we were on a phone we hid one from our View,
invoking the hide() method,
and we made it visible again invoking the show() method,
but both Fragments were always attached in the View tree and always existed in memory.
The approach that we're going to take now is going to be slightly different.
We are going to come in here for our... ...we come in here for a tablet approach that is going to stay identical:
we're going to statically define Activity A amber, the listFrag,
statically define Activity B blue, the detailFrag,
and both of those will always exist in memory when we are on a tablet.
The layout can really be identical to how it was when we were dealing with static Fragments
on this dynamic Fragment demo when we're dealing with a tablet, because the tablets the same.
We are always seeing both of them.
But then the thing we want to do differently is to say,
"Well when we're running on a phone I only want A there or I only want B there,
but I don't want A and B there both in memory at the same time.
When I have switched to B, I kill off A;
when I switch to A, I kill off B,
and there is only one Fragment in the View tree at one time.