Please enable JavaScript.
Coggle requires JavaScript to display documents.
Fragment :blossom: (Creating fragment
Dynamically:
1- Create a layout…
Fragment :blossom:
- Creating fragment
Dynamically:
1- Create a layout XML file for fragment.
2- Create fragment (java) by extending class of Fragment.
3- Set the layout XML file to the fragment.
4- Add a layout that will host the fragment and give it ID.
5- Write code to add fragment to Activity. And making the Fragment transaction by
- Adding
- Removing
- Replacing
-
-
-
Add
ft.add(R.id. layout - step 4- , fragment class name - step 2-)
ft.addToBackStack(null)
:fire: ft.commit()
- Mini-activities
- Chunk/ portion of UI.
- Has its own lifecycle. :fire:
- process its own events.
- can be added or removed during run time.
- fully encapsulated reusable components, each with its own lifcycle and UI.
- Dynamic - Static.
- self-contained.
- Design Philosophy:
- structure an activity as a collection of fragments.
- reuse fragment on different activities.
- Fragment's properties:
- Has its own life cycle.
- Has its own layout.
- can receive its own input events.
- can be added or removed during run time.
- Fragments VS Activities
- Fragments don't need to be registers in the mainfest file, while the activities must be exist in the mainfest file
- Fragments can be exist only when embedded into an activity
--> Fragment's life cycle depends on the activity that has been added to it.
- Activity has ViewGroup, which has the fragments
- the viewGroup is inside the activity's view hierarchy.
- onCreateView() --> draw the fragment's layout...
- Extend Fragment class or one of its sub classes:-
- DialogFragment.
- ListFragment.
- PreferenceFragment.
- WebViewFragment.
- Callback functions
- onAttach(Activity) --> once the fragment is associated with its activity.
- onCreate(Bundle) --> initial creation of the fragment.
- onCreateView(LayoutInflater, ViewGroup, Bundle) --> creates and returns the view hierarchy associated with the fragment.
- onActivityCreated(Bundle) -->tells the fragment that its activity has completed its own Activity.onCreate.
- onStrart() --> makes the fragment visible to the user.
- onResume() --> makes the fragment interacting with the user.
------------ Reverse of callback ------------
- onPause() --> no longer interacting with user.
- onStop() --> no longer visible.
- onDestroyView() --> clean up the resources.
- onDestroy() --> final clean up of the fragment states.
- onDetach() --> fragment no longer associated with its activity.
- XML::
- add <fragment> element inside activity layout
- Code:
- add the fragment into an existing ViewGroup.
-
- Fragment lifetime --> starts when it's bound to its parent activity, ends when its been de attached.
- onAttach --> onDetach
- Creating fragment
Statically:
1- Create a layout XML file for fragment.
2- Create fragment (java) by extending class of Fragment.
3- Set the layout XML file to the fragment.
4- Use <Fragment> tag to include the fragment in XML layout.
-- > android:name = ""
:fire: Not declared in the mainfest file, it just a part of the activity
FragmentMannager fm = getFragmentManager() -- > if extends Activity
or
FragementManager fm = getSupportFragmentManager() -- > if extends AppCompatActivity
FragmentTransaction ft = fm.beginTransaction()