This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll learn how to handle the Fragment back stack!
*** addToBackStack now works as expected ***
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
By default hitting the back
button only affects our activity.
0:00
If we want to go back
to our list fragment.
0:04
We'll need to learn a little bit
more about the fragment manager.
0:06
In addition to adding and
removing fragments the fragment manager is
0:10
also responsible for
managing the back stack of our fragments.
0:14
So, what does back stack mean?
0:17
Well.
0:19
The back stack is just a list
of fragment transactions.
0:20
Whenever we create a fragment transaction,
if we want to add it to the back stack.
0:24
We just use the addToBackStack method.
0:28
And provide a name for
this point in the back stack.
0:30
Then, when we would want to
go back to a certain point.
0:34
We can use the fragment managers pop
back stack method to jump back in time
0:37
to any of our named back stack entries.
0:42
Also, the back buttons default behavior
is to go back one fragment transaction
0:45
provided that we added that
transaction to the back stack.
0:50
Back in our code,
let's add a line after the call to replace
0:54
And call addToBackStack on
our fragment transaction.
1:00
And since we'll only be going
back one transaction at a time,
1:08
we don't really need to provide a name for
this entry.
1:12
So, let's just pass in null.
1:14
And then let's test the app again.
1:17
And if we click on a recipe and
1:25
then hit the back button,
we quit the app again.
1:28
Thanks Android,
1:33
this is another good example of
why people don't like fragments.
1:35
According to the docs.
1:39
This should work.
1:40
And what's more.
1:42
Figuring out how to make this
work correctly, isn't easy.
1:43
So, what's wrong?
1:47
Well, if you search for
ADD to back stack not working.
1:49
You'll see lots of responses,
1:52
saying that you need to override
the on back pressed method.
1:54
And just call, pop back stack yourself.
1:58
And while this does work,
it's more of a hack than a solution.
2:01
Instead, the issue is
that our fragments and
2:06
our activity aren't playing well together.
2:08
Remember, that when we're using fragments,
we can either use the regular fragment
2:11
class or
we can use the support fragment class.
2:15
Well, it turns out that
whichever one we pick
2:20
affects which type of
activity we should be using.
2:23
Check this out.
2:26
I'll change this from AppCompatActivity
to Activity and run the app.
2:28
Then I'll click on a recipe and
hit the back button.
2:41
And voila, it worked.
2:46
But we lost our action bar.
2:49
Any who, here's the takeaway.
2:52
Regular fragments should be
paired with the activity class.
2:54
And support fragments should be paired
with the app compact activity class.
2:58
So, what should we do
about our lost action bar.
3:03
We could add it back manually,
but instead,
3:06
let's just switch back to app compact
activity and use support fragments.
3:10
I'll quickly undo my change and
3:16
then let's change getFragmentManager
to getSupportFragmentManager.
3:18
And change it here, and here as well.
3:27
And let's delete the FragmentManager and
FragmentTransaction imports.
3:32
Then let's use ALT Enter to import
the support versions instead.
3:38
Next, let's head over to our ListFragment
class, delete the Fragment import,
3:46
and use ALT Enter to replace it
with the support version as well.
3:54
Then let's do the same thing for
our view pager fragment class.
4:01
Delete the fragment import and
import the support version.
4:05
Now let's test this one more time.
4:12
Looks like the action bar is back.
4:21
And if we click on a recipe and
then hit the back button.
4:23
We're back to our ListFragment.
4:29
Great work.
4:30
In the next video, we'll see how we can
move our toast into our ViewPagerFragment.
4:31
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up