2. WTF
what the fragment
Fragment is independent portion or chunk of a user interface in the
screen.
It was introduced in honeycomb to solve tablet design problems.
Has it’s own lifecycle methods.
Has backstacking functionalities.
Supports Multipane screen.
3. How different it from activity
Depends upon activities lifecycle.
Without hostactivity it fragment can not run.
You can add and remove fragments from running activity.
5. • OnAttach(): Called when the fragment has been associated with the activity (the Activity is passed in
here).
• onCreateView():Called to create the view hierarchy associated with the fragment.
• onActivityCreated():Called when the activity's onCreate() method has returned.
• onDestroyView():Called when the view hierarchy associated with the fragment is being removed.
• onDetach():Called when the fragment is being disassociated from the activity.
6. How many
ways to
create F ?
From xml using
<fragment />
01
Dynamically from
Activity with UI.
Returning view to the
activity.
02
Dynamically from
Activity without UI.
Returning view to the
activity.
03
7. public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
8. onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
1. Inflater : object to inflate the resource(xml)
2. Container : parent Viewgroup won which the fragment to be
attached.
3. savedInstanceState :bundle object to save the state of your
fragment .
10. How to
remove a F?
findFragmentById(int id)
01
findFragmentByTag(String
tag)
02
11. What else ?
add() :Add a fragment to the activity.
remove(): Remove a fragment from the activity. This operation destroys the
fragment instance unless the transaction is added to the transaction back stack,
described later.
replace() :Remove one fragment from the UI and replace it with another.
hide() : Hide a fragment in the UI (set its visibility to hidden without destroying
the view hierarchy).
show() :Show a previously hidden fragment.
detach() (API 13): Detach a fragment from the UI, destroying its view hierarchy
but retaining the fragment instance.
attach() (API 13): Reattach a fragment that has previously been detached from
the UI, re-creating its view hierarchy.
12. How to communicate with other
components.
Bundle you can pass the data.
Interfaces you can update / communicate new data.