- A Fragment represents a behavior or a portion of user interface in an Activity.
- A fragment must always be embedded in an activity and the fragment's life cycle is directly affected by the host activity's life cycle.
- You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup.
- We can also use a fragment without its own UI as an invisible worker for the activity.
- Fragments can be added, removed, replaced, and animated inside an Activity dynamically.
Fragment API Level
Fragment introduced in Honeycomb (Android 3.0) API level 11
Purpose
- support more dynamic and flexible UI designs on large screens
- promote re usability .
key classes
FragmentManager
FragmentTransaction
Step of
Creating Fragment
Step-1
Create a subclass of fragment
Create a layout for fragment
Implement the onCreateView() callback method to provide a user interface for a fragment
The inflate() method takes three arguments:
Step- 3
There are two way to add fragment into activity
Use to add the fragment run time.
- The resource ID of the layout you want to inflate.
- The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout parameters to the root view of the inflated layout, specified by the parent view in which it's going.
- A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation. (In this case, this is false because the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout.)
Step- 3
Adding
fragment into activity
There are two way to add fragment into activity
1. Declare the fragment
inside the activity's layout file.
Include fragment as a view inside your activity layout.
Include fragment as a view inside your activity layout.
Reference:http://developer.android.com/guide/components/fragments.html
Comments
Post a Comment