Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Updated 15 December 2016
Fragment are a part of an activity with a particular behavior.You can say it is some sort of sub-activity. It is a modular and reusable part of an activity , having there own life cycle but are still dependent on activity. You can add, remove, hide or show fragments according to your need in the activity.
You can use single as well as multiple , static as well as dynamic fragments in your activity based on your requirement. So firstly analyse your requirement and then use what suits you better.
Fragments are managed in an activity using FragmentManager for versions later Honeycomb below that the functionality is handled by FragmentActivity class. You can get Fragment manager object in an activity using
Fragment Transition:
Fragment Transition work is to perform operations on fragments. When a scene changes, a Transition has two main responsibilities −
User can add default animation as well as custom animation in fragments using Fragment Transaction.
Fragment Class:
As we have mentioned fragments have there own life cycle as well as layout so all the operations you want to perform are performed in its own .class file. You have to extend your MyFragment.class file from android.app.Fragment class. Then you have to override its lifecycle ments to get desired functionality.
onAttach(): Its the first method of the fragment that is called.
oncreate(): Its mainly used to initialize all components.
onCreateView(): Its called when its time draw the layout of your fragment.
onActivityCreated(): It is called when the activity is completely loaded and is ready for user interaction.
onPause(): Its called when user moves away from fragment and it is backstacked.
Static fragments:
Static fragments are added by adding fragment tag in activity layout XML file. The static fragments are add by default in your class you don’t need to call add() on the fragment.
You can get the fragment from its ID and perform operations like show or hide on it ( You can’t remove a static fragment from your activity).
Dynamic fragments:
Dynamic fragments are fragments loaded dynamically in the place of any layout (say container) in your activity layout XML. These require add() to be loaded in your activity and remove() will remove them from your activity.
Then you can add your fragment in the container as:
And when operations are to be performed on the fragment you can find the fragment using tag as passed in add().
So its the basics about fragments you can add many functionality in a fragment and use them as per your requirement and give your application a nice touch by adding animations in it. Also when fragments are hide using hide(), there state is saved so if you want to show user its last state you can hide fragment rather than removing it but please note that if activity is finished (destroyed) so do fragments associated with it.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.