0% found this document useful (0 votes)
90 views13 pages

Agment

Fragments allow reusable segments of UI to be included in activities. Fragments can be added statically in XML layouts or dynamically in code. Communication between fragments and activities can occur through callbacks passed when the fragment is created. The fragment lifecycle is similar to activities, with additional methods for attaching fragments to activities and retrieving the fragment view. Common fragment subclasses include DialogFragment for dialog boxes and ListFragment for displaying lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views13 pages

Agment

Fragments allow reusable segments of UI to be included in activities. Fragments can be added statically in XML layouts or dynamically in code. Communication between fragments and activities can occur through callbacks passed when the fragment is created. The fragment lifecycle is similar to activities, with additional methods for attaching fragments to activities and retrieving the fragment view. Common fragment subclasses include DialogFragment for dialog boxes and ListFragment for displaying lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Fragment

Contents
● What is Fragment
● Create Fragment
● Interact with Activity
Fragment
● Fragment: A reusable
segment of Android UI
that can appear in an
activity.
○ can help handle
different devices and
screen sizes
○ can reuse a common
fragment across
multiple activities
Create Fragment
Add Fragment to Activity
● Static way:
<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

<fragment android:name="com.example.news.ArticleListFragment"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</LinearLayout>
Add Fragment to Activity
● Dynamic way:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();

ExampleFragment fragment = new ExampleFragment();


fragmentTransaction.add(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
fragmentTransaction.commit();
Communicate with Activity
● From Activity to Fragment:
○ keep fragment instance in Activity and call fragment methods directly
● From Fragment to Activity
○ Pass a callback when the Fragment is created
public static class MainActivity extends Activity
public class HeadlinesFragment extends Fragment { implements OnHeadlineListener {
OnHeadlineListener callback; @Override
public void setOnHeadlineListener(OnHeadlineListener callback) public void onAttachFragment(Fragment fragment) {
if (fragment instanceof HeadlinesFragment) {
{
this.callback = callback; HeadlinesFragment headlinesFragment =
} (HeadlinesFragment) fragment;
public interface OnHeadlineListener {
public void onArticleSelected(int position);
} headlinesFragment.setOnHeadlineListener(this);
} }
Fragment lifecycle
● Fragments have a similar life cycle and events as
activities.
● Important methods:
○ onAttach to glue fragment to its surrounding activity
○ onCreate when fragment is loading
○ onCreateView method that must return fragment's root UI view
○ onActivityCreated method that indicates the enclosing activity is
ready
○ onPause when fragment is being left/exited
○ onDetach just as fragment is being deleted
Fragment lifecycle
● Fragment in an Activity---Activity Lifecyle influences
○ Activity paused -> all its fragments paused
○ Activity destroyed -> all its fragments destroyed
○ Activity running -> manipulate each fragment independently.
● Fragment transaction: add, remove, etc.
○ adds it to a back stack that's managed by the activity—each back stack
entry in the activity is a record of the fragment transaction that occurred.
○ The back stack allows the user to reverse a fragment transaction
(navigate backwards), by pressing the Back button
Fragment lifecycle
Fragment subclasses
● DialogFragment - a fragment meant to be
shown as a dialog box that pops up on top of
the current activity.
● ListFragment - a fragment that shows a list
of items as its main content.
● PreferenceFragment - a fragment whose
main content is meant to allow the user to
change settings for the app
Classwork
● Using Fragment to make:
○ A ListProduct Fragment
○ A DetailedProduct Fragment

● Click an Item in ListProject Fragment, show DetailedProduct


Fragment
References
● https://google-developer-training.github.io/android-developer-fun
damentals-course-concepts-v2/

You might also like