How to Troubleshoot Apps for the Modern Connected Worker
Advanced Android Fragments, Tom Opgenorth
1. Tom Opgenorth
Developer / Writer
Xamarin
tom.opgenorth@xamarin.com
Fragments - The Next Step
(Advanced Fragments)
@topgenorth
2. Topics to Look out For In this Session
• Quick Review of Fragments
• How Fragments can Help Their Host with State
• Not ONE But TWO Fragments :
DialogFragment
PreferenceFragment
4. What Do We Need to Remember About
Fragments ?
• Fragments Are Modular UI
Think “mini-Activity”
• Hosted in an Activity
Statically - in Layout
Dynamically - at Runtime
• They inflate views (usually)
• They have a lifecycle
6. • Runtime / Changes kill Activities
State needs to be maintained
• Custom objects to maintain state
Subclass Java.Lang.Object
Override OnRetainNonConfigurationInstance
Check Activity.LastNonConfigurationInstance
But these are DEPRECATED!
Activity State Can Complex
7. - Leonardo da Vinci
Simplicity is the Ultimate Sophistication
9. How Does this Impact the Fragment
Lifecycle?
Start
OnInflate OnAttach
OnCreate
OnCreate
View
OnActivity
Created
OnStart
OnResume
OnPause
OnStop
OnDestroy
View
OnDestroy
OnDetachRunning
End
10. • Survives config changes!
• Lightweight
• Perfect for
Background Process (Threads)
Memory intensive Resources
Sharing Data
What Does This Mean - Exactly?
13. Why Use DialogFragment?
• Configuration changes are hard on dialogs
Dialog Should Remain - but the Activity is destroyed
• Customizable
• “Managed Dialogs” - Old School
14. How Do I Use DialogFragments?
• Subclass DialogFragment
• Override one of the following methods:
OnCreateDialog
• A “proper” dialog will pop up
OnCreateView
• Inflate a View - then embed it
• Modal, but not blocking
17. PreferenceFragment : Why Do I Care?
• Preferred Way for Android 3.0 or Higher
• Can Share Across Activities
• Allows for Hierarchies
PreferenceActivity Does Not
22. What Were We Talking About Again?
• Keep Fragments around with RetainInstance
• DialogFragments
Dialog or Embedded
Modal but not Blocking!
• PreferenceFragment