SlideShare a Scribd company logo
WORKING WITH FRAGMENTS
Android Developer Days 2013
Can Elmas
Saturday, June 15, 13
ABOUT ME
• Pozitron, since 2007
• Backend and client side experience
• SoftwareTeam Leader at the moment
Saturday, June 15, 13
AGENDA
• Overview
• When to Use?
• Managing Fragments
• Fragment Operations
• Demo(s)
Saturday, June 15, 13
OVERVIEW
• Introduced with Honeycomb (API 11)
• Modular section of an activity / Part of a user interface*
• More dynamic and flexible UI on large screens
• Reusability across activities
• Supported via Compatibility Package
* can also exist without user interface; Headless fragments
Saturday, June 15, 13
OVERVIEW
• Embedded in activities
• Lives in aViewGroup inside the activity’s view hierarchy
• Its own layout and behavior with its own life-cycle
Saturday, June 15, 13
WHENTO USE?
• Large screens - multi pane, flexible UI
• Combined with other UI widgets, ActionBar,ViewPager,
NavigationDrawer etc. - beautiful native app!
• More flexible, smooth, compact app with less activities;
multiple fragments in a single activity
Saturday, June 15, 13
http://developer.android.com/guide/components/fragments.html
Use Case -Tablet vs Handset
Saturday, June 15, 13
Use Case - Quora App Action Bar Navigation
Saturday, June 15, 13
Use Case - Quora AppView Pager
swipe
Saturday, June 15, 13
Use Case -YouTube Navigation Drawer
Saturday, June 15, 13
Saturday, June 15, 13
onCreate(Bundle) called once the fragment is
associated with its activity. Activity is still in the process
of being created.
Do not depend on activity’s layout
onCreateView(LayoutInflater, ViewGroup,
Bundle) time to instantiate for the fragment’s user
interface (optional, return null for non-graphical
fragments)
onActivityCreated(Bundle) called once the
activity is created and fragment’s view is instantiated.
Do final initializations (context dependent instantiations,
retrieving views, adding listeners etc.)
onDestroyView() Called when the view has been
detached from the fragment. Next time the fragment
needs to be displayed, a new view will be created.
Saturday, June 15, 13
MANAGING FRAGMENTS
• FragmentManager to interact with fragments inside an
Activity
• Activity.getFragmentManager()
• android.support.v4.app.FragmentActivity.getSupportFragmentMa
nager()
• FragmentTransaction to perform fragment operations :
add, remove, replace, hide at run-time etc.
Saturday, June 15, 13
ADDING FRAGMENTS -
STATIC
• <fragment> element in the activity’s layout XML
• Preferable if the fragment is consistent in the layout
• Limits run-time fragment operations; can’t remove
Saturday, June 15, 13
DEMO
Saturday, June 15, 13
TAKE AWAYS
• Use Support Library for backward compatibility
• android.support.v4.app.FragmentActivity
• android.support.v4.app.Fragment
• FragmentActivity.getSupportFragmentManager()
• Use different layouts to support multiple screen sizes
• Static Instantiation limits run time fragment operations
Saturday, June 15, 13
TAKE AWAYS
• Fragment to Fragment communication via callbacks to the
Activity; inner type interfaces defined in fragments
• Avoid tight coupling between activities and fragments
• To access activity from fragment, Fragment.getActivity()
• To access fragment from activity
• FragmentManager.findFragmentById(int id)
• FragmentManager.findFragmentByTag(String tag)
Saturday, June 15, 13
FRAGMENT OPERATIONS
• add - Add a fragment to the activity
• remove - Remove an existing fragment; its view is also
removed
• replace - Remove one fragment from the activity and add
another
• hide - Hides an existing fragment; its view is hidden
• show - Show previously hidden fragment
Saturday, June 15, 13
FRAGMENTTRANSACTIONS
• commit() resolves in the main thread
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container, fragment);
ft.commit();
Saturday, June 15, 13
FRAGMENT BACK STACK
• Similar to activity back stack
• Allow user to navigate backward
• Ability to save fragment transaction onto back stack
• Managed by activity on back button press
• Activity destroyed once all fragment transactions removed
from the back stack and back button is pressed again
Saturday, June 15, 13
FRAGMENT BACK STACK
• FragmentTransaction.addToBackStack(String)
• null or optional identifier name for the back stack state
• Useful for returning to a given state by calling
FragmentManager’s popBackStack methods
• popBackStack() : Pop the top state off the back stack
• popBackStack(String name, int flags) : pop all states up to the
named state.Also pops this state if
POP_BACK_STACK_INCLUSIVE flag is set.
Saturday, June 15, 13
FRAGMENT BACK STACK
Fragment A
ft.add(R.id.fragment_container, fragmentA);
ft.addToBackStack("fragStackA")
Fragment A
Fragment B1
1.
2.
Fragment A
Fragment B
Fragment C2
ft.add(R.id.fragment_container, fragmentB);
3
ft.add(R.id.fragment_container, fragmentC);3.
Saturday, June 15, 13
FRAGMENT BACK STACK
Fragment A Fragment A
Fragment B1
Fragment A
Fragment B
Fragment C2 3
4.
4
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack("fragStackA", 0);
Saturday, June 15, 13
FRAGMENT BACK STACK
Fragment A Fragment A
Fragment B1
Fragment A
Fragment B
Fragment C2 3
4.
4
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack("fragStackA", POP_BACK_STACK_INCLUSIVE);
Saturday, June 15, 13
DEMO
Saturday, June 15, 13
TAKE AWAYS
• Depending on your requirements choose one of the paths :
• add add add...
• replace - navigating back to a removed and back stacked
state triggers onCreateView() to be called again
• hide/add - might require keep tracking of current fragment
to hide
• re-draw or re-initialize ?
Saturday, June 15, 13
TAKE AWAYS
• Add fragment transactions to back stack when necessary
• Use custom animations for fragment transactions :
FragmentTransaction.setCustomAnimations(enter, exit, popEnter,
popExit)
• ActionBar,ViewPager, MapFragment to provide beautiful,
flexible and more native user interfaces
Saturday, June 15, 13
TAKE AWAYS
• ActionBarSherlock (http://actionbarsherlock.com)
• extension to support library to provide action bar design
across all Android versions
• SherlockFragmentActivity
• SherlockFragment
Saturday, June 15, 13
TAKE AWAYS
• ViewPager
• available with support library
• flip left/right through pages of data
• SupportMapFragment
• google_play_service.jar required
• google play services should be installed on device
Saturday, June 15, 13
TAKE AWAYS
• Update action bar state (title, options items etc.) from
fragments
• "static factory method" to initialize fragments
• Pass data to fragments via Fragment.setArguments(Bundle) and
Fragments.getArguments()
public static FragSpeaker newInstance(Speaker speaker) {
FragSpeaker fragment = new FragSpeaker();
Bundle data = new Bundle();
data.putSerializable(KEY_SPEAKER, speaker);
fragment.setArguments(data);
return fragment;
}
Saturday, June 15, 13
FROM HERE
• Nested Fragments (with support library revision11) -
fragments inside fragments; child fragments
• DialogFragment
• PreferenceFragment - similar visual style of system preferences
• Headless Fragments; without UI
Saturday, June 15, 13
Slides and source code
https://github.com/canelmas/add2013
Saturday, June 15, 13
THANKYOU!
We are hiring!
ik@pozitron.com
http://www.pozitron.com/career/
can.elmas@pozitron.com
twitter : can_elmas
Saturday, June 15, 13

More Related Content

What's hot

Android Fragment
Android FragmentAndroid Fragment
Android Fragment
Kan-Han (John) Lu
 
Explanation onAttach() of Fragment class in Android
Explanation onAttach() of Fragment class in AndroidExplanation onAttach() of Fragment class in Android
Explanation onAttach() of Fragment class in Android
yuchi_1k91 Pit
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
Prawesh Shrestha
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
Henry Osborne
 
Android basic 4 Navigation Drawer
Android basic 4 Navigation DrawerAndroid basic 4 Navigation Drawer
Android basic 4 Navigation Drawer
Eakapong Kattiya
 
Android basic 2 UI Design
Android basic 2 UI DesignAndroid basic 2 UI Design
Android basic 2 UI Design
Eakapong Kattiya
 
Android basic 3 Dialogs
Android basic 3 DialogsAndroid basic 3 Dialogs
Android basic 3 Dialogs
Eakapong Kattiya
 
Android Components
Android ComponentsAndroid Components
Android Components
Aatul Palandurkar
 
Android
AndroidAndroid
Android
Pranav Ashok
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
Osahon Gino Ediagbonya
 
android activity
android activityandroid activity
android activity
Deepa Rani
 
Android development - Activities, Views & Intents
Android development - Activities, Views & IntentsAndroid development - Activities, Views & Intents
Android development - Activities, Views & Intents
Lope Emano
 
STYLISH FLOOR
STYLISH FLOORSTYLISH FLOOR
STYLISH FLOOR
ABU HASAN
 
Android best practices
Android best practicesAndroid best practices
Android best practices
Jose Manuel Ortega Candel
 
Android UI Fundamentals part 1
Android UI Fundamentals part 1Android UI Fundamentals part 1
Android UI Fundamentals part 1
Marcos Paulo Souza Damasceno
 
Andriod dev toolbox part 2
Andriod dev toolbox  part 2Andriod dev toolbox  part 2
Andriod dev toolbox part 2
Shem Magnezi
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andevAdvanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
Mike Nakhimovich
 
Android Support Library
Android Support LibraryAndroid Support Library
Android Support Library
Alexey Ustenko
 
Android in practice
Android in practiceAndroid in practice
Android in practice
Jose Manuel Ortega Candel
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
Jussi Pohjolainen
 

What's hot (20)

Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
Explanation onAttach() of Fragment class in Android
Explanation onAttach() of Fragment class in AndroidExplanation onAttach() of Fragment class in Android
Explanation onAttach() of Fragment class in Android
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Android basic 4 Navigation Drawer
Android basic 4 Navigation DrawerAndroid basic 4 Navigation Drawer
Android basic 4 Navigation Drawer
 
Android basic 2 UI Design
Android basic 2 UI DesignAndroid basic 2 UI Design
Android basic 2 UI Design
 
Android basic 3 Dialogs
Android basic 3 DialogsAndroid basic 3 Dialogs
Android basic 3 Dialogs
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Android
AndroidAndroid
Android
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
android activity
android activityandroid activity
android activity
 
Android development - Activities, Views & Intents
Android development - Activities, Views & IntentsAndroid development - Activities, Views & Intents
Android development - Activities, Views & Intents
 
STYLISH FLOOR
STYLISH FLOORSTYLISH FLOOR
STYLISH FLOOR
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
Android UI Fundamentals part 1
Android UI Fundamentals part 1Android UI Fundamentals part 1
Android UI Fundamentals part 1
 
Andriod dev toolbox part 2
Andriod dev toolbox  part 2Andriod dev toolbox  part 2
Andriod dev toolbox part 2
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andevAdvanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
 
Android Support Library
Android Support LibraryAndroid Support Library
Android Support Library
 
Android in practice
Android in practiceAndroid in practice
Android in practice
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 

Viewers also liked

Android Fragment-Awesome
Android Fragment-AwesomeAndroid Fragment-Awesome
Android Fragment-Awesome
GauntFace
 
Android 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndroid 101 - Introduction to Android Development
Android 101 - Introduction to Android Development
Andy Scherzinger
 
Android Fragment Pattern: Communication
Android Fragment Pattern: CommunicationAndroid Fragment Pattern: Communication
Android Fragment Pattern: Communication
zmontesd
 
Fragment debugging
Fragment debuggingFragment debugging
Fragment debugging
Meghaditya Roy Chaudhury
 
Spinners, Adapters & Fragment Communication
Spinners, Adapters & Fragment CommunicationSpinners, Adapters & Fragment Communication
Spinners, Adapters & Fragment Communication
CITSimon
 
Google I/O 2011, Android Honeycomb Highlights
Google I/O 2011, Android Honeycomb HighlightsGoogle I/O 2011, Android Honeycomb Highlights
Google I/O 2011, Android Honeycomb Highlights
Romain Guy
 
Screen orientations in android
Screen orientations in androidScreen orientations in android
Screen orientations in android
manjakannar
 
Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)
Khaled Anaqwa
 
Fragments notes powerpoint
Fragments notes powerpointFragments notes powerpoint
Fragments notes powerpoint
ktyndall
 
Intent in android
Intent in androidIntent in android
Intent in android
Durai S
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Ahsanul Karim
 
Android presentation
Android presentationAndroid presentation
Android presentation
Elyes Naouar
 
Fragmentation and types of fragmentation in Distributed Database
Fragmentation and types of fragmentation in Distributed DatabaseFragmentation and types of fragmentation in Distributed Database
Fragmentation and types of fragmentation in Distributed Database
Abhilasha Lahigude
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
CodeAndroid
 
Android tutorial ppt
Android tutorial pptAndroid tutorial ppt
Android tutorial ppt
Rehna Renu
 
Gaming Technology Presentation
Gaming Technology PresentationGaming Technology Presentation
Gaming Technology Presentation
MrQaz996
 
The Emerging Virtual Reality Landscape: a Primer
The Emerging Virtual Reality Landscape: a PrimerThe Emerging Virtual Reality Landscape: a Primer
The Emerging Virtual Reality Landscape: a Primer
Sim Blaustein
 
Video Game Powerpoint
Video Game PowerpointVideo Game Powerpoint
Video Game Powerpoint
Nari07
 
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo SilvaAndroid | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
JAX London
 

Viewers also liked (19)

Android Fragment-Awesome
Android Fragment-AwesomeAndroid Fragment-Awesome
Android Fragment-Awesome
 
Android 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndroid 101 - Introduction to Android Development
Android 101 - Introduction to Android Development
 
Android Fragment Pattern: Communication
Android Fragment Pattern: CommunicationAndroid Fragment Pattern: Communication
Android Fragment Pattern: Communication
 
Fragment debugging
Fragment debuggingFragment debugging
Fragment debugging
 
Spinners, Adapters & Fragment Communication
Spinners, Adapters & Fragment CommunicationSpinners, Adapters & Fragment Communication
Spinners, Adapters & Fragment Communication
 
Google I/O 2011, Android Honeycomb Highlights
Google I/O 2011, Android Honeycomb HighlightsGoogle I/O 2011, Android Honeycomb Highlights
Google I/O 2011, Android Honeycomb Highlights
 
Screen orientations in android
Screen orientations in androidScreen orientations in android
Screen orientations in android
 
Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)Android Training (Storing & Shared Preferences)
Android Training (Storing & Shared Preferences)
 
Fragments notes powerpoint
Fragments notes powerpointFragments notes powerpoint
Fragments notes powerpoint
 
Intent in android
Intent in androidIntent in android
Intent in android
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
 
Android presentation
Android presentationAndroid presentation
Android presentation
 
Fragmentation and types of fragmentation in Distributed Database
Fragmentation and types of fragmentation in Distributed DatabaseFragmentation and types of fragmentation in Distributed Database
Fragmentation and types of fragmentation in Distributed Database
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
 
Android tutorial ppt
Android tutorial pptAndroid tutorial ppt
Android tutorial ppt
 
Gaming Technology Presentation
Gaming Technology PresentationGaming Technology Presentation
Gaming Technology Presentation
 
The Emerging Virtual Reality Landscape: a Primer
The Emerging Virtual Reality Landscape: a PrimerThe Emerging Virtual Reality Landscape: a Primer
The Emerging Virtual Reality Landscape: a Primer
 
Video Game Powerpoint
Video Game PowerpointVideo Game Powerpoint
Video Game Powerpoint
 
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo SilvaAndroid | Android Activity Launch Modes and Tasks | Gonçalo Silva
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
 

Similar to Android - Working with Fragments

Lecture #4 activities &amp; fragments
Lecture #4  activities &amp; fragmentsLecture #4  activities &amp; fragments
Lecture #4 activities &amp; fragments
Vitali Pekelis
 
Fragmentation in android
Fragmentation in android Fragmentation in android
Fragmentation in android
Esraa El Ghoul
 
Fragments In Android
Fragments In AndroidFragments In Android
Fragments In Android
DivyaKS12
 
fragments-activity.pptx
fragments-activity.pptxfragments-activity.pptx
fragments-activity.pptx
SriKGangadharRaoAssi
 
Android design patterns
Android design patternsAndroid design patterns
Android design patterns
Platty Soft
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
Gabor Varadi
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
Ahsanul Karim
 
04 activities and activity life cycle
04 activities and activity life cycle04 activities and activity life cycle
04 activities and activity life cycle
Sokngim Sa
 
Tk2323 lecture 6 fragment (new)
Tk2323 lecture 6   fragment (new)Tk2323 lecture 6   fragment (new)
Tk2323 lecture 6 fragment (new)
MengChun Lam
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
CITSimon
 
What the fragments
What the fragmentsWhat the fragments
What the fragments
Gowtham Kumar
 
Android Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompatAndroid Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompat
cbeyls
 
Bundling Client Side Assets
Bundling Client Side AssetsBundling Client Side Assets
Bundling Client Side Assets
Timothy Oxley
 
Multi screenlab
Multi screenlabMulti screenlab
Multi screenlab
Ran Nachmany
 
Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS
Sandeep Sharma IIMK Smart City,IoT,Bigdata,Cloud,BI,DW
 
深入淺出談Fragment
深入淺出談Fragment深入淺出談Fragment
深入淺出談Fragment
毅 方
 
jQuery Mobile Deep Dive
jQuery Mobile Deep DivejQuery Mobile Deep Dive
jQuery Mobile Deep Dive
Troy Miles
 
User experience and interactions design
User experience and interactions design User experience and interactions design
User experience and interactions design
Rakesh Jha
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScript
Codemotion
 
mDevCamp - The Best from Google IO
mDevCamp - The Best from Google IOmDevCamp - The Best from Google IO
mDevCamp - The Best from Google IO
ondraz
 

Similar to Android - Working with Fragments (20)

Lecture #4 activities &amp; fragments
Lecture #4  activities &amp; fragmentsLecture #4  activities &amp; fragments
Lecture #4 activities &amp; fragments
 
Fragmentation in android
Fragmentation in android Fragmentation in android
Fragmentation in android
 
Fragments In Android
Fragments In AndroidFragments In Android
Fragments In Android
 
fragments-activity.pptx
fragments-activity.pptxfragments-activity.pptx
fragments-activity.pptx
 
Android design patterns
Android design patternsAndroid design patterns
Android design patterns
 
Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)Architecting Single Activity Applications (With or Without Fragments)
Architecting Single Activity Applications (With or Without Fragments)
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
04 activities and activity life cycle
04 activities and activity life cycle04 activities and activity life cycle
04 activities and activity life cycle
 
Tk2323 lecture 6 fragment (new)
Tk2323 lecture 6   fragment (new)Tk2323 lecture 6   fragment (new)
Tk2323 lecture 6 fragment (new)
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
What the fragments
What the fragmentsWhat the fragments
What the fragments
 
Android Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompatAndroid Support Library: Using ActionBarCompat
Android Support Library: Using ActionBarCompat
 
Bundling Client Side Assets
Bundling Client Side AssetsBundling Client Side Assets
Bundling Client Side Assets
 
Multi screenlab
Multi screenlabMulti screenlab
Multi screenlab
 
Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS Mobile application development: part 1: Andriod Vs IOS
Mobile application development: part 1: Andriod Vs IOS
 
深入淺出談Fragment
深入淺出談Fragment深入淺出談Fragment
深入淺出談Fragment
 
jQuery Mobile Deep Dive
jQuery Mobile Deep DivejQuery Mobile Deep Dive
jQuery Mobile Deep Dive
 
User experience and interactions design
User experience and interactions design User experience and interactions design
User experience and interactions design
 
Reactive Programming with JavaScript
Reactive Programming with JavaScriptReactive Programming with JavaScript
Reactive Programming with JavaScript
 
mDevCamp - The Best from Google IO
mDevCamp - The Best from Google IOmDevCamp - The Best from Google IO
mDevCamp - The Best from Google IO
 

Recently uploaded

Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 

Recently uploaded (20)

Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 

Android - Working with Fragments

  • 1. WORKING WITH FRAGMENTS Android Developer Days 2013 Can Elmas Saturday, June 15, 13
  • 2. ABOUT ME • Pozitron, since 2007 • Backend and client side experience • SoftwareTeam Leader at the moment Saturday, June 15, 13
  • 3. AGENDA • Overview • When to Use? • Managing Fragments • Fragment Operations • Demo(s) Saturday, June 15, 13
  • 4. OVERVIEW • Introduced with Honeycomb (API 11) • Modular section of an activity / Part of a user interface* • More dynamic and flexible UI on large screens • Reusability across activities • Supported via Compatibility Package * can also exist without user interface; Headless fragments Saturday, June 15, 13
  • 5. OVERVIEW • Embedded in activities • Lives in aViewGroup inside the activity’s view hierarchy • Its own layout and behavior with its own life-cycle Saturday, June 15, 13
  • 6. WHENTO USE? • Large screens - multi pane, flexible UI • Combined with other UI widgets, ActionBar,ViewPager, NavigationDrawer etc. - beautiful native app! • More flexible, smooth, compact app with less activities; multiple fragments in a single activity Saturday, June 15, 13
  • 8. Use Case - Quora App Action Bar Navigation Saturday, June 15, 13
  • 9. Use Case - Quora AppView Pager swipe Saturday, June 15, 13
  • 10. Use Case -YouTube Navigation Drawer Saturday, June 15, 13
  • 12. onCreate(Bundle) called once the fragment is associated with its activity. Activity is still in the process of being created. Do not depend on activity’s layout onCreateView(LayoutInflater, ViewGroup, Bundle) time to instantiate for the fragment’s user interface (optional, return null for non-graphical fragments) onActivityCreated(Bundle) called once the activity is created and fragment’s view is instantiated. Do final initializations (context dependent instantiations, retrieving views, adding listeners etc.) onDestroyView() Called when the view has been detached from the fragment. Next time the fragment needs to be displayed, a new view will be created. Saturday, June 15, 13
  • 13. MANAGING FRAGMENTS • FragmentManager to interact with fragments inside an Activity • Activity.getFragmentManager() • android.support.v4.app.FragmentActivity.getSupportFragmentMa nager() • FragmentTransaction to perform fragment operations : add, remove, replace, hide at run-time etc. Saturday, June 15, 13
  • 14. ADDING FRAGMENTS - STATIC • <fragment> element in the activity’s layout XML • Preferable if the fragment is consistent in the layout • Limits run-time fragment operations; can’t remove Saturday, June 15, 13
  • 16. TAKE AWAYS • Use Support Library for backward compatibility • android.support.v4.app.FragmentActivity • android.support.v4.app.Fragment • FragmentActivity.getSupportFragmentManager() • Use different layouts to support multiple screen sizes • Static Instantiation limits run time fragment operations Saturday, June 15, 13
  • 17. TAKE AWAYS • Fragment to Fragment communication via callbacks to the Activity; inner type interfaces defined in fragments • Avoid tight coupling between activities and fragments • To access activity from fragment, Fragment.getActivity() • To access fragment from activity • FragmentManager.findFragmentById(int id) • FragmentManager.findFragmentByTag(String tag) Saturday, June 15, 13
  • 18. FRAGMENT OPERATIONS • add - Add a fragment to the activity • remove - Remove an existing fragment; its view is also removed • replace - Remove one fragment from the activity and add another • hide - Hides an existing fragment; its view is hidden • show - Show previously hidden fragment Saturday, June 15, 13
  • 19. FRAGMENTTRANSACTIONS • commit() resolves in the main thread FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.fragment_container, fragment); ft.commit(); Saturday, June 15, 13
  • 20. FRAGMENT BACK STACK • Similar to activity back stack • Allow user to navigate backward • Ability to save fragment transaction onto back stack • Managed by activity on back button press • Activity destroyed once all fragment transactions removed from the back stack and back button is pressed again Saturday, June 15, 13
  • 21. FRAGMENT BACK STACK • FragmentTransaction.addToBackStack(String) • null or optional identifier name for the back stack state • Useful for returning to a given state by calling FragmentManager’s popBackStack methods • popBackStack() : Pop the top state off the back stack • popBackStack(String name, int flags) : pop all states up to the named state.Also pops this state if POP_BACK_STACK_INCLUSIVE flag is set. Saturday, June 15, 13
  • 22. FRAGMENT BACK STACK Fragment A ft.add(R.id.fragment_container, fragmentA); ft.addToBackStack("fragStackA") Fragment A Fragment B1 1. 2. Fragment A Fragment B Fragment C2 ft.add(R.id.fragment_container, fragmentB); 3 ft.add(R.id.fragment_container, fragmentC);3. Saturday, June 15, 13
  • 23. FRAGMENT BACK STACK Fragment A Fragment A Fragment B1 Fragment A Fragment B Fragment C2 3 4. 4 FragmentManager fm = getSupportFragmentManager(); fm.popBackStack("fragStackA", 0); Saturday, June 15, 13
  • 24. FRAGMENT BACK STACK Fragment A Fragment A Fragment B1 Fragment A Fragment B Fragment C2 3 4. 4 FragmentManager fm = getSupportFragmentManager(); fm.popBackStack("fragStackA", POP_BACK_STACK_INCLUSIVE); Saturday, June 15, 13
  • 26. TAKE AWAYS • Depending on your requirements choose one of the paths : • add add add... • replace - navigating back to a removed and back stacked state triggers onCreateView() to be called again • hide/add - might require keep tracking of current fragment to hide • re-draw or re-initialize ? Saturday, June 15, 13
  • 27. TAKE AWAYS • Add fragment transactions to back stack when necessary • Use custom animations for fragment transactions : FragmentTransaction.setCustomAnimations(enter, exit, popEnter, popExit) • ActionBar,ViewPager, MapFragment to provide beautiful, flexible and more native user interfaces Saturday, June 15, 13
  • 28. TAKE AWAYS • ActionBarSherlock (http://actionbarsherlock.com) • extension to support library to provide action bar design across all Android versions • SherlockFragmentActivity • SherlockFragment Saturday, June 15, 13
  • 29. TAKE AWAYS • ViewPager • available with support library • flip left/right through pages of data • SupportMapFragment • google_play_service.jar required • google play services should be installed on device Saturday, June 15, 13
  • 30. TAKE AWAYS • Update action bar state (title, options items etc.) from fragments • "static factory method" to initialize fragments • Pass data to fragments via Fragment.setArguments(Bundle) and Fragments.getArguments() public static FragSpeaker newInstance(Speaker speaker) { FragSpeaker fragment = new FragSpeaker(); Bundle data = new Bundle(); data.putSerializable(KEY_SPEAKER, speaker); fragment.setArguments(data); return fragment; } Saturday, June 15, 13
  • 31. FROM HERE • Nested Fragments (with support library revision11) - fragments inside fragments; child fragments • DialogFragment • PreferenceFragment - similar visual style of system preferences • Headless Fragments; without UI Saturday, June 15, 13
  • 32. Slides and source code https://github.com/canelmas/add2013 Saturday, June 15, 13