Tuesday, December 2, 2014

Fragment in android (available from 3.0)

It is essentially grouping of UI and the interaction the user can have with that. Here fragment is somewhat similar to model of MVC architecture.
It is not possible to show two activity at the same time because each activity have their own life cycle. If u have shown one that is active other will be inactive.



In above figure shown that two activity needed to display the list and details view . In phone needed two different activity because size is less. In tablet we can show two view in one time so There will be duplicasy of code if same thing used for phone and tablet.
In above case difficult two maintain two code.
The solution to above is fragment .  Fragment is essentially grouping of UI and interaction with user have with that. Fragment is similar to the model in mVC.
Here is solution for above problem.  List fragment is list and Detail fragment for detail view. In phone different activity uses fragment in Tablet one activity uses both fragment.



In phone list activity displays lis fragment and detail activity detail fragment. in tablet both fragment by one custom activity.
Fragment can be made up of one or more views. Fragment can save their own state. So will resume also. 

Om a single screen you can display any number of fragment. Fragment can be reusable. Fragment can be made up of one or more number of views. Fragments typically handle the configuration changes so they can save and restore the ROM state.
In android for view you can use the xml. Fragment may contain layout . 
Layout may also have multiple fragments. There is fragment manager which is     manages the back press.


Compatiablity SDK is there which takes the classes that are available in specific version and make some available to older version. So we can use the same SDK if we want to support the older sdk.

No comments:

Post a Comment