Skip to main content

Basic understanding of Android Fragments and its State



What is Fragment and why did it come into existence?


Fragment is one of the highly used class/component in the current mobile and Tab application development. Its has increased the development and usability up to a right scale.  Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity's appearance at runtime and preserve those changes in a back stack that's managed by the activity.

For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in the tablet layout in figure 1.






Fragment States :

Fragment class has almost similar class as Activity class. Overall an activity contains the 7 7 life cycle state and a Fragment contains 11 lifecycle state.

Activity States : 


  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onRestart()
  7. onDestroy()
To Understand the full flow use the following link.

http://developer.android.com/reference/android/app/Activity.html

Fragment States:
  1. onAttach()
  2. onCreate()
  3. onCreateView()
  4. onActivityCreated()
  5. onStart()
  6. onResume()
  7. onPause()
  8. onStop()
  9. onDestroyView()
  10. onDestroy()
  11. onDetach()

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 detached from Activity.






Comments

Popular posts from this blog

Design Android Tool bar with constraint layout

 Toolbar in Android plays a very important role in quick page movement.  Steps to implement toolbar in Android 1. Create a layout file toolbar.xml in r es/layout directory 2. add below code into your toolbar.xml file. 3. Customize this as per your requirement <? xml version ="1.0"  encoding ="utf-8" ?> <layout xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: app ="http://schemas.android.com/apk/res-auto" xmlns: tools ="http://schemas.android.com/tools" > <androidx.appcompat.widget.Toolbar android :id ="@+id/toolbar" android :layout_width ="match_parent" android :layout_height ="?attr/actionBarSize" android :background ="@color/colorPrimaryDark" app :elevation ="0dp" app :layout_collapseMode ="pin" > <androidx.constraintlayout.widget.ConstraintLayout android ...

Android Studio ADB is not responding manually kill adb Server

I was using Ubuntu 14.04. After installing Android Studio, I started getting this error. After all possible updates it still din't work. I had become crazy in solving this issue.                             After executing this command this error got resolved. First Command : adb apt-get install lib32stdc++6  Second Command : sudo apt-get install lib32z1 For other Adb related issue  

A Complete Nodejs boiler plate - This man need no introduction

Node.js  is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. A highly used solution in current mobile and cloud environment. Node.js allows the creation of  Web servers  and networking tools using  JavaScript  and a collection of "modules" that handle various core functionality. Modules are provided for  file system  I/O, networking ( DNS ,  HTTP ,  TCP ,  TLS/SSL , or  UDP ),  binary  data (buffers),  cryptography  functions,  data streams , and other core functions. Node.js's modules use an API designed to reduce the complexity of writing server applications. Nodejs is a powerful platform. Its being used across multiple applications.  Looking at the usability of Nodejs I have created a nodejs boilter plate for beginners and professionals. You can plugin play your required features. This...