Skip to Content
Android Application Development Cookbook - Second Edition
book

Android Application Development Cookbook - Second Edition

by Rick Boyer, Kyle Mew
March 2016
Intermediate to advanced
428 pages
9h 2m
English
Packt Publishing
Content preview from Android Application Development Cookbook - Second Edition

Adding and removing Fragments during runtime

Defining a Fragment in the layout, as we did in the previous recipe, is known as a static Fragment and cannot be changed during runtime. Rather than using the <fragment> element, we will create a container to hold the Fragment, then create the Fragment dynamically in the Activity's onCreate() method.

The FragmentManager provides the APIs for adding, removing, and changing Fragments during runtime using a FragmentTransaction. A Fragment transaction consists of:

  • Starting a transaction
  • Performing one or multiple actions
  • Committing the transaction

This recipe will demonstrate the FragmentManager by adding and removing Fragments during runtime.

Getting ready

Create a new project in Android Studio and call it: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Android 9 Development Cookbook - Third Edition

Android 9 Development Cookbook - Third Edition

Rick Boyer

Publisher Resources

ISBN: 9781785886195Supplemental Content