Android Studio Fragment Introduction

In this Android Studio article iam going to have a simple Introduction to Android

Fragment, also we are going to talk about creation of Fragment , so  for good explanation

you can watch the video for this article at the end .  

 

 

 

Also you can read more android development articles

1: Android Development Articles

 

 

 

Also you can check Python GUI Development Tutorials in the below link.

1: PyQt5 GUI Development Tutorials

2: TKinter GUI Development Tutorials

3: Pyside2 GUI Development Tutorials 

4: Kivy GUI Development Tutorials

5: wxPython GUI Development Tutorials 

 

 

 

What are Fragments ?

Fragment is a piece of an activity which enable more modular activity design. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts.
Android devices exists in a variety of screen sizes and densities. Fragments simplify the reuse of components in different layouts and their logic. You can build single-pane layouts for handsets (phones) and multi-pane layouts for tablets. You can also use fragments also to support different layout for landscape and portrait orientation on a smartphone.

The below image shows how two UI modules defined by fragments can be combined into one activity for a tablet design but separated for a handset design.

 

 

 

Fragment Life cycle 

Android fragments have their own life cycle very similar to an android activity.

  • onAttach() : The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.
  • onCreate() : The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
  • onCreateView() : The system calls this callback when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.
  • onActivityCreated() : The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object
  • onStart() : The onStart() method is called once the fragment gets visible.
  • onResume() : Fragment becomes active.
  • onPause() : The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.
  • onStop() : Fragment going to be stopped by calling onStop()
  • onDestroyView() : Fragment view will destroy after call this method
  • onDestroy() :called to do final clean up of the fragment’s state but Not guaranteed to be called by the Android platform.

 

 

So there are two ways that you can create fragments, the first way that you can add a fragment to your main activity through xml file , and the second way is that you can add fragment to the main activity dynamically. in this article we are going to focus on adding fragment through xml file.

 

 

 

 

1: First of all create a new project in your Android Studio, and choose Empty Activity also iam using API 20 for this project.

2: After that you need to create two fragments in android studio, basically when you create a fragment there will be two files the first one is the xml file for the fragment and the second one the .java file for the fragment in fragment the class of java file extends from Fragment. so now this is the code for our two fragments, and we are going to add just a simple TextView in the fragment.

 

 

 

 

and this is the second fragment.

 

 

 

 

So we have created the two fragments and we don’t need to bring changes in the .java file, now it is time to add these two fragments in our main_activity.xml file. it is easy and simple, just you need to open your  main_activity.xml and add a fragment tag in the xml file, the important attribute is the name attribute that you need to refer to that fragment like this.

 

 

 

 

 

 

 

So now run the project in android studio and this will be the result.

Android Studio Fragment Introduction
Android Studio Fragment Introduction

 

 

 

 

 

Also you can watch the complete video for this article

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Comments are closed.

Share via
Copy link
Powered by Social Snap
×