
Android Fragments: A Comprehensive Guide
Learn about Android fragments, their lifecycle, creation, addition to activities, transactions, and communication with activities. Discover how to create flexible and dynamic UI designs using fragments in Android development.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Fragments: Introduction Fragments were introduced in Android 3.0 to support flexible and dynamic UI designs represent portions of an application s user interface Each fragment would contain its own set of views Have their own life cycle, receiving their own input events And can be added and removed to an activity at run time Their lifecycles are affected by the activity creating them If activity paused => all its associated fragments are paused as well If activity destroyed => associated fragments are also destroyed
Fragments: Creation To create a fragment You must subclass the Fragment class and override onCreateView() The method called to draw the user interface of a fragment Example: 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); } }
Adding a Fragment to an activity A fragment contributes a portion of the UI to host activity At any time, one can add fragments to an activity By using the FragmentTransaction class and By specifying a ViewGroup in which to place fragment Example FragmentManager fragmentManager = getFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();
Fragment Transactions Fragment transactions Are performed by means of the FragmentTransaction class Each transaction is a set of changes performed at the same time And can be reversed by saving it to the back stack Example: // Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit();
Fragments: Communicating with Activity A fragment is independent of an activity But, can be used inside multiple activities With a given instance being directly tied to the activity Containing the fragment Can access its associated activity using getActivity() Likewise, an activity can access a fragment By acquiring a reference to the fragment via FragmentManager ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
Fragments: FragmentManagementApp Refer to FragmentManagementApp project
Fragments: Life Cycle Like activities, fragments Have their own life cycle When a fragment is created onAttach, onCreate, onCreateView onActivityCreated becomes visible onStart, onResume goes into background mode onPause, onStop is destroyed onPause, onStop, onDestroyView, onDestroy, on Detach