Tip:
Highlight text to annotate it
X
0:00:00.000,0:00:09.033
This video explores fragments in Android, it's part 1
of a 7 part series. We start with a broad overview
and then move into static fragments.
0:00:09.033,0:00:13.033
The objectives are stated here with a time line for
when they appear in the video.
0:00:31.000,0:00:37.066
OK so the answer to the question,
"What's the coolest thing in Android?"
when asked, is "Fragments."
0:00:37.066,0:00:41.066
So I am going to get into exactly what Fragments
are in a moment.
0:00:41.066,0:00:48.033
But quickly, they were given birth basically for the
need for Android
to support multiple screen sizes, as sometimes you
have a tablet,
0:00:52.033,0:00:58.000
sometimes you have a phone, and you want your
application to look great in both environments.
0:00:58.000,0:01:02.066
And Fragments are one of the very cool tools
we can use to solve that problem.
0:01:02.066,0:01:08.000
Before we get into a more in-depth discussion
about what Fragments actually are and how to use
them,
0:01:08.000,0:01:18.033
it would be helpful if you had gone out to the
developer website and read this link on Fragments
in its entirety.
0:01:18.033,0:01:21.066
It would be also helpful if you went out to the
developer web site
0:01:21.066,0:01:27.066
and at least browsed through this document that
talks about supporting multiple screen sizes.
0:01:27.066,0:01:33.033
OK. So while you were reading (and I'm assuming
now at this point you have done that)
0:01:33.033,0:01:37.000
...so while you were reading the document on
Fragments,
0:01:37.000,0:01:41.033
you will see that there's a statement there
that says,
0:01:41.033,0:01:46.033
"Android introduced Fragments in Android 3.0 (API
11)."
0:01:46.033,0:01:53.033
If we left the statement right there, that would mean
that you can't use Fragments unless you are
running on API 11.
0:01:53.033,0:01:59.033
So if you are going to use Fragments, you have to
to build against API 11 (Android 3.0) and you've
now said,
0:01:59.033,0:02:06.033
"Any device that's running a version of Android
earlier than API 11 will not be able to use my
application,
0:02:06.033,0:02:10.000
because I'm building against 3.0 (API 11)."
0:02:10.000,0:02:13.066
Well OK, let's see how that affects us.
0:02:13.066,0:02:19.033
If we go out here and look at this current distribution
chart that I am pulling up on July 9, 2012,
0:02:19.033,0:02:26.033
we go, "Well, that's a bad idea." Because we can
come out here and look at this pie chart, going,
0:02:26.033,0:02:31.000
"I've only got this little area to deal with (Android 3.0
and above)
0:02:31.000,0:02:37.033
and I alienate me from all of the rest if I went ahead
and made that (API 11) decision."
0:02:37.033,0:02:43.033
So unless you are building a very specialized
application to only run on a tablet,
0:02:43.033,0:02:48.066
and you don't care about the phone market,
you are not likely, at the point where you would
make a decision,
0:02:48.066,0:02:52.066
to build an application that is just going to run
against IAndroid) 3.0 and above.
0:02:52.066,0:02:59.066
But yet Fragments are really cool, and they allow
you to do so much to support multiple screen sizes.
0:02:59.066,0:03:02.033
What they did with the Android operating system is,
0:03:02.033,0:03:08.033
they allowed the ability to take some really cool
features introduced in Android 3.0 and above
0:03:08.033,0:03:11.066
(such as Fragments and Loaders and a few other
things)
0:03:11.066,0:03:18.000
and put them in what they call a Support Package
(or Compatibility Package, I have seen it under
both names).
0:03:18.000,0:03:21.033
If you add that Support/Compatibility package
0:03:21.033,0:03:24.033
(I will just call it the Support Package from now on)
0:03:24.033,0:03:31.033
to your application, it takes some of the features
that are in Android 3.0
0:03:31.033,0:03:37.033
and allows you to use them actually back all the
way to Android 1.6.
0:03:37.033,0:03:39.033
So that is the approach we are going to take,
0:03:39.033,0:03:41.033
as I am going to say I am going to build against
2.3.
0:03:41.033,0:03:46.033
I guess we're really not doing anything that
wouldn't allow us to build against 2.2
and add that piece of the pie back in,
0:03:46.033,0:03:50.000
but I have just been building against 2.3.3
for most of these demos.
0:03:50.000,0:03:52.000
I will continue to do that.
0:03:52.000,0:03:56.000
And we will go, "Let's use that Compatibility
Package
0:03:56.000,0:04:04.066
so we can use the Fragments that were added
in API 11 (back in an earlier API)."
0:04:04.066,0:04:06.066
So that is the first step we need to take.
0:04:06.066,0:04:11.066
In order to do that,
in your Eclipse, if you have not already,
0:04:11.066,0:04:14.000
you need to do this:
go up to your SDK Manager...
0:04:14.000,0:04:20.033
And, by the way, in mine I have deployed
"Jellybean" (Android 4.1, API 16).
0:04:20.033,0:04:28.066
That was released as I've been doing this video
series, and I think I am going to build a tablet
against that for this demo today.
0:04:28.066,0:04:32.000
At any rate, you need to look down
and make sure that you have installed,
under "Extras,"
0:04:32.000,0:04:34.033
the Android Support Library.
0:04:34.033,0:04:40.066
That will add the option to put that Android Support
Library in your application
0:04:40.066,0:04:47.033
so that you will be able to use Fragments without
having to build against Android 3.0 and above,
keeping your application open to a much larger
base of devices.
0:04:51.066,0:04:57.033
If you have not installed this, go ahead and install
that in your Eclipse.
0:04:57.033,0:05:01.000
Before we get started in Eclipse, I just wanted to
give you a vision of where we're going.
0:05:01.000,0:05:06.000
In the reading on Fragments on the Android
Developer website we see this common problem
0:05:06.000,0:05:10.066
(that is a problem we are going to be solving and,
in fact, solving three times)
0:05:10.066,0:05:13.033
and that is the classic "list view-detail view"
0:05:13.033,0:05:17.066
(I see a list of items here, and when I click at it I
want to see additional details over here).
0:05:17.066,0:05:23.066
Of course when we are running on a tablet (a larger
device), we have room to see both at the same
time.
0:05:23.066,0:05:27.066
On a smaller device, we need to swap out one view
for the other
0:05:27.066,0:05:30.066
because there is simply not enough room to show
both.
0:05:30.066,0:05:35.066
And that is one thing Fragments were just
designed to help us do. And when we do that,
0:05:35.066,0:05:38.033
we are going to solve this in three different ways.
0:05:38.033,0:05:40.033
So let me just discuss those up front.
0:05:40.033,0:05:44.033
This outer box that I am going to draw here
represents an Activity.
0:05:44.033,0:05:47.066
I am just going to call that "Activity A."
0:05:47.066,0:05:52.033
Then inside that Activity, I am going to draw what I
call "Fragment A."
0:05:52.033,0:05:55.000
Then I am going to draw what I call "Fragment B."
0:05:55.000,0:05:58.000
Of course, this (largest box "A") just represents a
single Activity,
0:05:58.000,0:06:01.066
this (smaller box "A") represents what we might
consider to be our list view,
0:06:01.066,0:06:06.000
and this (smaller box "B") might be what we
understand be our detail view.
0:06:06.000,0:06:11.000
When we are running on a tablet, we can see both
of those at the same time, and everything's good.
0:06:11.000,0:06:13.000
But when we are on a phone, we don't have the
screen real estate for that.
0:06:13.000,0:06:16.000
Then of course I might not be running on a tablet,
0:06:16.000,0:06:21.000
I might be running on a phone, where my Activity
has to be much smaller (my Activity A),
0:06:21.000,0:06:28.066
and then I might only have room to show Fragment
A on my Activity A at one time (my list Fragment)
0:06:28.033,0:06:35.033
and then when I click on some item I want to see
more details about in Fragment A,
0:06:35.033,0:06:41.033
I need to replace the space taken up by Fragment
A with Fragment B.
0:06:41.033,0:06:47.066
The first approach that I am going to take to do that
is going to be using what is called "static
Fragments."
0:06:47.066,0:06:53.000
Both of these Fragments here (A) and here (B),
0:06:53.000,0:06:56.000
...and both of these Fragments, Fragment A and
Fragment B,
0:06:56.000,0:07:00.033
that I never actually displayed at the same time,
are going to be statically declared,
0:07:00.033,0:07:03.000
and they are both actually always in memory.
0:07:03.000,0:07:09.000
(Even though when running on this example we
may not see both Fragments at the same time,
0:07:09.000,0:07:13.033
both Fragments will actually exist in memory
at the same time.)
0:07:13.033,0:07:16.033
The first example is going to be the use of static
Fragments.
0:07:16.033,0:07:19.066
That is perhaps the easiest way to solve the
problem.
0:07:19.066,0:07:22.066
Then we're going to move on and say,
0:07:22.066,0:07:25.033
"Well, we want understand Fragments a little
deeper."
0:07:25.033,0:07:27.066
And we are going to solve the same problem
again,
0:07:27.066,0:07:30.033
this time using what's called dynamic Fragments.
0:07:30.033,0:07:42.033
Instead of statically (here's Activity A and again
here is Fragment A and Fragment B taking up our
screen real estate
0:07:42.033,0:07:44.033
and again we are showing that on a tablet).
0:07:44.033,0:07:49.066
But what we are going to do here is, instead of
statically declaring these, we are going to
dynamically create them.
0:07:49.066,0:08:01.033
We're going to do a little work programmatically
to place Fragment A on Activity A
and to place Fragment B on Activity B.
0:08:01.033,0:08:05.033
Then also when running on a phone
where we have more limited screen real estate,
0:08:05.033,0:08:13.000
we are going to say, "Yeah, I am on Activity A
and Fragment A is there,
0:08:13.000,0:08:18.066
and I click on some ListView, and I want to see the
details.
0:08:18.066,0:08:25.000
Under the previous example, I simply can hide
Activity (Fragment) A, but it's still in memory.
0:08:25.000,0:08:29.000
And then I show Activity (Fragment) B with Activity
(Fragment) A remaining in memory.
0:08:29.000,0:08:33.000
But under this example, I'm actually going to
remove
0:08:33.000,0:08:37.033
the dynamically created Fragment A,
0:08:37.033,0:08:43.033
and replace that with a new dynamically created
Fragment B, (and Fragment B).
0:08:43.033,0:08:46.000
(Fragment B) will then be put in its (A's) place.
0:08:46.000,0:08:49.066
Very similar scenarios for those two:
0:08:49.066,0:08:53.066
one using static Fragments, both in memory,
with kind of a show-and-hide scenario;
0:08:53.066,0:08:59.066
and one using dynamic Fragments where both
aren't actually in memory at the same time.
0:08:59.066,0:09:03.066
So keep in mind in those two approaches
we only ever had a single Activity
0:09:03.066,0:09:09.000
that hosted sometimes two Fragments and
sometimes one.
0:09:09.000,0:09:13.033
Then the final approach we are going to take is
going to be a two-Activity approach,
0:09:13.033,0:09:18.000
where when we're on, again, a tablet (a larger
device),
0:09:18.000,0:09:28.000
we are going to have Activity A hosting
Fragment A and Fragment B.
0:09:28.000,0:09:34.066
When we are on a large device like a tablet,
the only thing is going to come into play is a single
Activity, Activity A.
0:09:34.066,0:09:41.033
And Activity A is going to show Fragment A (the list
view) and Fragment B, the detail view.
0:09:41.033,0:09:46.000
However, under this final approach we are going to
say
0:09:46.000,0:09:51.066
"If we have limited screen real estate,
we're going to use the two-Activity model.
0:09:51.066,0:09:58.000
Activity Awill host Fragment A (the detail view),
0:09:58.000,0:10:04.033
and we will also have an Activity B that hosts
Fragment B.
0:10:04.033,0:10:11.066
And of course we will only need Activity B
to host Fragment B
0:10:11.066,0:10:16.033
when running on a device with limited screen real
estate such as on a phone.
0:10:16.033,0:10:21.033
So we're going to solve this problem three times
with those three approaches.
0:10:21.033,0:10:26.000
Through the course of that, we will discover much of
the functionality that is important to us about
Fragments.
0:10:26.000,0:10:30.066
OK. So a couple important things when you start
this project...
0:10:30.066,0:10:37.033
Again, remember that you must have come in
and clicked on your Android SDK Manager,
0:10:37.033,0:10:42.000
and made sure the Android Support Library is
shown as installed.
0:10:42.000,0:10:46.000
And when you create that project...
0:10:46.000,0:10:50.066
I am going to call this "FragmentDemo,
0:10:50.066,0:10:54.066
I will just stay building against 2.3.3 and 2.3.3,
0:10:54.066,0:11:00.066
I'm going to create a custom icon
using my Launcher,
0:11:00.066,0:11:05.033
I think I like my background color actually to be
black for that.
0:11:05.033,0:11:09.033
We will go ahead and give it a blank Activity to start
with,
0:11:09.033,0:11:14.033
call it "MainActivity," and just call its XML layout file
"main," and choose "Finish."
Then one thing you want to make sure you do on
this project from the very beginning
0:11:19.033,0:11:28.033
is click on that project folder,
go down to the Android Tools and say,
"Add Support Library..."