SlideShare uma empresa 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

Mais conteúdo relacionado

Destaque

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
 

Destaque (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
 
Компас юного петербуржца
Компас юного петербуржцаКомпас юного петербуржца
Компас юного петербуржца
 

Mais 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
 

Mais 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

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
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
 
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
 

Último (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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
 
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...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
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
 
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
 

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