Mobile App Development Tutorial How To Manage Multiple Fragments On Single Activity


1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)



Android comes with an activity stack and we don’t need to maintain the stack while opening or closing an activity. Android will automatically handle the stack and show you the top activity when you press the Back button. But it is necessary to handle fragments explicitly because Android is not going to handle them automatically. For example, some applications might require something like the left sliding menus or the bottom tab bar that has multiple items with a particular work flow in each.

Here, I would like to share with you how you can handle this using a single activity and fragment container as stack.

Tutorial How To Manage Multiple Fragments On Single Activity


Tutorial How To Manage Multiple Fragments On Single Activity 


1      Defining all of menu/tab types

You should create an enum object to define all of menu/tab types that will be showed in your application.



2      Creating base activity class.

2.1     Creating fragment container

To store fragments of all menus/tabs, we need to use Map object as container. Each element of it consists of a pair of key and value in which key is type of menu/tab and value is stack of fragments. We will push and pop the fragment into corresponding stack.


2.2     Setting the current menu/tab

It is active menu/tab that will be operated then.



2.3     Pushing new fragment

Whenever you want to display any new fragment, just push that fragment into stack using following code.



2.4     Poping the current fragment

Whenever you want to destroy the current fragment and go back to previous fragment, just pop that fragment using following code without sending data to previous fragment


Here you can pop a fragment and send data to previous fragment


2.5     Pressing Back button

When the backPressed event fires, we will check whether stack size of current menu/tab is <=1 or not. If it is not, then we will pop last fragment and display the previous fragment by following code.



3      Create running activity

To use this helper, we just create any activity and extend the base class above. The Inheritance class overrides getFragmentContainerResId() method to determine component will use to push/pop fragment.




4      Example



With this post, I hope you can be more confident using fragments in your application implementation with stack fragment manager.


Brought to you by the RobustTechHouse team. RobustTechHouse works on mobile app development projects and eCommerce & web development projects. If you like our articles, please also check out our Facebook page.

Recommended Posts
  • Aromal Sasidharan

    Can i get source code of this example? please let me know

Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
digital transformation_01Smartchain_01