SlideShare una empresa de Scribd logo
1 de 5
"Android Application Development Company India" www.letsnurture.com
Navigation with Back-stack in
Android App Development
"Android Application Development Company India" www.letsnurture.com
Android Fragment Backstack.
Fragments are useful when we want to support multiple screen size. To
managefragments we need a FragmentManager that help us to handle
trasaction between fragments. With transaction we mean a sequence of
steps to add, replace or remove fragments.
To load Fragment dynamic way
We have to use FrameLayout. With FrameLayout we can handle
fragments as we need at runtime, but to do it we need a manager, in other
words a component that can handle fragments.This is FragmentManager.
This component can add, replace and remove fragments at runtime.
Link to Example With Navigation Drawer
Calander Tutorial
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Displaying Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
"Android Application Development Company India" www.letsnurture.com
android:layout_height="match_parent"
/>
<!-- Displaying Drawer -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/drawer_list_selection"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>
FragmentManager
As we said before FragmentManager is the key component. Using
FragmentManager we can discover (find) fragment inside our layout using
findFragmentById or findFragmentByTag.While the first method is very
simple and we use the common android id to discover the component, the
second method (that uses tag) is unsual. A tag in a fragment is simply a
“name” we give to the fragment so that we can find it later using that name.
All the operation that are made by the FragmentManager happens inside a
“transaction” like in a database operation. First, we can get the
"Android Application Development Company India" www.letsnurture.com
FragmentManger using the Activity method getFragmentManager(). Once
we have the reference to this component we have to start the transaction in
this way:
// get referance
FragmentManager fragmentManager1 = getFragmentManager();
// begin transaction
fragmentManager1.beginTransaction();
// Transaction commint
fragmentManager1.commit();
At the end when we finished and we are ready to show our fragment we have to
call the commit method that marks the end of the transaction.
Fragment fragment1 = new Fragment1Nasdaq();
FragmentManager fragmentManager1 = getFragmentManager();
fragmentManager1.beginTransaction().add(R.id.frame_container,
fragment1).commit();
We can perform following operations:
"Android Application Development Company India" www.letsnurture.com
 add a new fragment
 replace an existing fragment
 remove a fragment
Fragment Backstack
The back button acts at activity level not at the fragment level. So our activity is
the same while we replace fragments as the user interacts with the app. In this
way when we tap on the back button we select the first activity on the activity
stack, in our case the home. We don’t want this behaviour but we want that
when we click on the back button we go back in the fragments stack. We can
achieve it adding the fragment to the backstack. We do it in this way:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container,
fragment).addToBackStack("").commit();
We use the addToBackStack method of the FragmentTrasaction and we add
every fragment to the backstack. Call addToBackStack() before you commit the
transaction. In this way when we tap on the back button we have the correct
behaviour.
When there are FragmentTransaction objects on the back stack and the user
presses the Back button theFragmentManager pops the most recent transaction
off the back stack and performs the reverse action(such as removing a fragment
if the transaction added it).
Check Example of Fragment Back Stack

Más contenido relacionado

Destacado

Fragments notes powerpoint
Fragments notes powerpointFragments notes powerpoint
Fragments notes powerpointktyndall
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android FragmentsSergi Martínez
 
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 SilvaJAX London
 
November Public Meetings
November Public MeetingsNovember Public Meetings
November Public MeetingsMatt Mansell
 
Futurizm kartinki
Futurizm kartinkiFuturizm kartinki
Futurizm kartinkikryljanauki
 
Ohionetegovojfswebinar
OhionetegovojfswebinarOhionetegovojfswebinar
OhionetegovojfswebinarShelly Miller
 
Operations Management briefing
Operations Management briefingOperations Management briefing
Operations Management briefingSandeep Premnath
 
структура сказки для 5 класса
структура сказки для 5 классаструктура сказки для 5 класса
структура сказки для 5 классаkryljanauki
 
Holes (themes)
Holes (themes)Holes (themes)
Holes (themes)salmaowen
 
Blog installation in magento
Blog installation in magentoBlog installation in magento
Blog installation in magentoKetan Raval
 
Компас юного петербуржца
Компас юного петербуржцаКомпас юного петербуржца
Компас юного петербуржцаkryljanauki
 

Destacado (17)

Fragment
Fragment Fragment
Fragment
 
Fragments notes powerpoint
Fragments notes powerpointFragments notes powerpoint
Fragments notes powerpoint
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 
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
 
November Public Meetings
November Public MeetingsNovember Public Meetings
November Public Meetings
 
Angie rodriguez 4,9
Angie rodriguez 4,9Angie rodriguez 4,9
Angie rodriguez 4,9
 
Empresa
EmpresaEmpresa
Empresa
 
Futurizm kartinki
Futurizm kartinkiFuturizm kartinki
Futurizm kartinki
 
Sant jordi
Sant jordiSant jordi
Sant jordi
 
Ohionetegovojfswebinar
OhionetegovojfswebinarOhionetegovojfswebinar
Ohionetegovojfswebinar
 
Operations Management briefing
Operations Management briefingOperations Management briefing
Operations Management briefing
 
структура сказки для 5 класса
структура сказки для 5 классаструктура сказки для 5 класса
структура сказки для 5 класса
 
Assignment brief
Assignment briefAssignment brief
Assignment brief
 
Holes (themes)
Holes (themes)Holes (themes)
Holes (themes)
 
AHA Application
AHA Application AHA Application
AHA Application
 
Blog installation in magento
Blog installation in magentoBlog installation in magento
Blog installation in magento
 
Компас юного петербуржца
Компас юного петербуржцаКомпас юного петербуржца
Компас юного петербуржца
 

Más de Ketan Raval

Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Ketan Raval
 
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Ketan Raval
 
Zero ui future is here
Zero ui   future is hereZero ui   future is here
Zero ui future is hereKetan Raval
 
Android n and beyond
Android n and beyondAndroid n and beyond
Android n and beyondKetan Raval
 
IoT and Future of Connected world
IoT and Future of Connected worldIoT and Future of Connected world
IoT and Future of Connected worldKetan Raval
 
#Instagram API Get visibility you always wanted
#Instagram API   Get visibility you always wanted#Instagram API   Get visibility you always wanted
#Instagram API Get visibility you always wantedKetan Raval
 
Keynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKeynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKetan Raval
 
Android notifications
Android notificationsAndroid notifications
Android notificationsKetan Raval
 
How to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantHow to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantKetan Raval
 
3 d touch a true game changer
3 d touch a true game changer3 d touch a true game changer
3 d touch a true game changerKetan Raval
 
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyOBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyKetan Raval
 
Vehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsVehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsKetan Raval
 
Obd how to guide
Obd how to guideObd how to guide
Obd how to guideKetan Raval
 
Garmin api integration
Garmin api integrationGarmin api integration
Garmin api integrationKetan Raval
 
Beacon The Google Way
Beacon The Google WayBeacon The Google Way
Beacon The Google WayKetan Raval
 
Edge detection iOS application
Edge detection iOS applicationEdge detection iOS application
Edge detection iOS applicationKetan Raval
 
Google calendar integration in iOS app
Google calendar integration in iOS appGoogle calendar integration in iOS app
Google calendar integration in iOS appKetan Raval
 
Big data cloudcomputing
Big data cloudcomputingBig data cloudcomputing
Big data cloudcomputingKetan Raval
 
All about Apple Watchkit
All about Apple WatchkitAll about Apple Watchkit
All about Apple WatchkitKetan Raval
 

Más de Ketan Raval (20)

Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)
 
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
 
Keynote 2016
Keynote 2016Keynote 2016
Keynote 2016
 
Zero ui future is here
Zero ui   future is hereZero ui   future is here
Zero ui future is here
 
Android n and beyond
Android n and beyondAndroid n and beyond
Android n and beyond
 
IoT and Future of Connected world
IoT and Future of Connected worldIoT and Future of Connected world
IoT and Future of Connected world
 
#Instagram API Get visibility you always wanted
#Instagram API   Get visibility you always wanted#Instagram API   Get visibility you always wanted
#Instagram API Get visibility you always wanted
 
Keynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKeynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG Ahmedabad
 
Android notifications
Android notificationsAndroid notifications
Android notifications
 
How to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantHow to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA Compliant
 
3 d touch a true game changer
3 d touch a true game changer3 d touch a true game changer
3 d touch a true game changer
 
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyOBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
 
Vehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsVehicle to vehicle communication using gps
Vehicle to vehicle communication using gps
 
Obd how to guide
Obd how to guideObd how to guide
Obd how to guide
 
Garmin api integration
Garmin api integrationGarmin api integration
Garmin api integration
 
Beacon The Google Way
Beacon The Google WayBeacon The Google Way
Beacon The Google Way
 
Edge detection iOS application
Edge detection iOS applicationEdge detection iOS application
Edge detection iOS application
 
Google calendar integration in iOS app
Google calendar integration in iOS appGoogle calendar integration in iOS app
Google calendar integration in iOS app
 
Big data cloudcomputing
Big data cloudcomputingBig data cloudcomputing
Big data cloudcomputing
 
All about Apple Watchkit
All about Apple WatchkitAll about Apple Watchkit
All about Apple Watchkit
 

Último

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Último (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Navigation with back stack in android app development

  • 1. "Android Application Development Company India" www.letsnurture.com Navigation with Back-stack in Android App Development
  • 2. "Android Application Development Company India" www.letsnurture.com Android Fragment Backstack. Fragments are useful when we want to support multiple screen size. To managefragments we need a FragmentManager that help us to handle trasaction between fragments. With transaction we mean a sequence of steps to add, replace or remove fragments. To load Fragment dynamic way We have to use FrameLayout. With FrameLayout we can handle fragments as we need at runtime, but to do it we need a manager, in other words a component that can handle fragments.This is FragmentManager. This component can add, replace and remove fragments at runtime. Link to Example With Navigation Drawer Calander Tutorial <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Displaying Fragments --> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent"
  • 3. "Android Application Development Company India" www.letsnurture.com android:layout_height="match_parent" /> <!-- Displaying Drawer --> <ListView android:id="@+id/list_slidermenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@color/list_divider" android:dividerHeight="1dp" android:listSelector="@drawable/drawer_list_selection" android:background="@color/list_background"/> </android.support.v4.widget.DrawerLayout> FragmentManager As we said before FragmentManager is the key component. Using FragmentManager we can discover (find) fragment inside our layout using findFragmentById or findFragmentByTag.While the first method is very simple and we use the common android id to discover the component, the second method (that uses tag) is unsual. A tag in a fragment is simply a “name” we give to the fragment so that we can find it later using that name. All the operation that are made by the FragmentManager happens inside a “transaction” like in a database operation. First, we can get the
  • 4. "Android Application Development Company India" www.letsnurture.com FragmentManger using the Activity method getFragmentManager(). Once we have the reference to this component we have to start the transaction in this way: // get referance FragmentManager fragmentManager1 = getFragmentManager(); // begin transaction fragmentManager1.beginTransaction(); // Transaction commint fragmentManager1.commit(); At the end when we finished and we are ready to show our fragment we have to call the commit method that marks the end of the transaction. Fragment fragment1 = new Fragment1Nasdaq(); FragmentManager fragmentManager1 = getFragmentManager(); fragmentManager1.beginTransaction().add(R.id.frame_container, fragment1).commit(); We can perform following operations:
  • 5. "Android Application Development Company India" www.letsnurture.com  add a new fragment  replace an existing fragment  remove a fragment Fragment Backstack The back button acts at activity level not at the fragment level. So our activity is the same while we replace fragments as the user interacts with the app. In this way when we tap on the back button we select the first activity on the activity stack, in our case the home. We don’t want this behaviour but we want that when we click on the back button we go back in the fragments stack. We can achieve it adding the fragment to the backstack. We do it in this way: FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack("").commit(); We use the addToBackStack method of the FragmentTrasaction and we add every fragment to the backstack. Call addToBackStack() before you commit the transaction. In this way when we tap on the back button we have the correct behaviour. When there are FragmentTransaction objects on the back stack and the user presses the Back button theFragmentManager pops the most recent transaction off the back stack and performs the reverse action(such as removing a fragment if the transaction added it). Check Example of Fragment Back Stack