When you're using a ViewPager to manage fragments, you don't have direct access to the FragmentTransaction objects that are being used in the background. One consequence is that you can't just add your fragments to the app's backstack. But you can simulate backstack behavior by overriding the onBackButton() method, and determining whether to go back to a previous fragment or to let the back button's normal behavior take over.
- [Instructor] I've previously described…how to use a ViewPager to allow a user…to swipe through data elements in a data set,…but I also described a behavioral issue.…When the user presses the back button,…they might expect to go back to the previous page,…but instead, they exit the application.…You might think you can use the back stack to manage this.…The problem, though, is that even though…you're using fragment transaction,…the way those transactions are created within a ViewPager…is hidden from you, and you can't easily modify it.…
So instead, you can intercept the event that happens…when the user presses the back button,…and change the behavior explicitly.…I'm working in a version of the project…named PagerBackButton and I'm starting…in the MainActivity class.…I'll override the method onBackPressed.…The default behavior is just to call…the Superclass's version of this same method,…and that's the code that's exiting the application.…I'm going to nest that bit of code in a conditional block,…and I'll ask the question, am I on the first page…
Author
Released
9/1/2016- Configuring Android Studio
- Understanding fragments
- Creating a fragment class and layout
- Adding and removing fragments with Java
- Creating layouts for multiple screens
- Understanding arguments and callback methods
- Passing arguments to a fragment
- Choosing layout at runtime
- Displaying dialogs with fragments
- Using fragments for managing dialogs, shared preferences, and more
Skill Level Intermediate
Duration
Views
-
Introduction
-
Welcome1m 4s
-
What you should know1m 50s
-
Using the exercise files1m 39s
-
-
1. Getting Started
-
Understanding fragments3m 17s
-
2. Display Fragments in Activities
-
Display a fragment with XML3m 16s
-
Add a fragment with Java3m 50s
-
3. Manage Fragments at Runtime
-
Pass arguments to a fragment5m 18s
-
Choose layout at runtime3m 21s
-
-
4. Communication between Fragments and Activities
-
5. Display Dialogs with Fragments
-
Display a custom dialog5m 1s
-
Pass arguments to the dialog4m 16s
-
6. Other Uses of Fragments
-
Create a ViewPager adapter4m 53s
-
Conclusion
-
Next steps1m 11s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Manage back button with ViewPager