Introduction to Fragment
Android Fragment is the part of the activity, it’s also called sub-activity. There may be multiple fragments in an activity. Fragments represent more than one display screen inner one activity.
What is a fragment in android?
A Fragment is a reusable part of your app’s user interface. A fragment has its own layout and lifecycle, as well as the ability to handle its own input events. Fragments cannot exist without the assistance of activity or another fragment.
Difference between Activity and Fragment
Activity is a component of an application that provides a user interface that users can interact with. The code snippet is only part of the operation, it is mainly added to your UI. The fragment depends on the activity. After using multiple fragments in an activity, you can create a multi-screen user interface.
Introduction to Fragment Lifecycle
Methods:
Methods | Description |
---|---|
onCreate() | In this method onCreate() initialize the main component and fragment variables in this callback. The system calls this method when creating a fragment. |
onCreateView() | In this method onCreateView () inflate the XML layout for the fragment in this callback. The system calls this method to draw the Fragment user interface for the first time. |
onViewCreated() | In this method onViewCreated() Is called immediately after onCreateView (LayoutInflater, ViewGroup, Bundle) has returned |
onstart() | The onStart () method is called as soon as the fragment becomes visible. |
onResume() | onResume () is called when your activity becomes active, you don’t need to call onResume () on a fragment. |
onPause() | This method is called when the user interface is partially visible to the user. When a dialog in the activity is opened, the activity goes into the pause state and calls the onPause () method |
onStop() | By calling onStop () method the Fragment stop |
onSaveInstanceState() | This method closes the gap between user expectations and system behavior. |
onDestroyView() | This method allows the fragment to clean up the resources associated with its View. |
onDestroy() | This method call to finally clean up the state of the fragments. |
Types of Fragment
- Single Frame Fragment:
Single Frame fragments -> are used for handheld devices such as cell phones, here we can only display a fragment as a view.
- List Fragment:
List Fragments -> that have a special list, the view is called list fragments
- Fragment transaction:
Fragment transaction -> Using with fragment transaction. we will circulate one fragment to some other fragment.
Preview: Fragment Example
File: MainActivity.kt
package com.example.fragmentsbehaviour import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import androidx.fragment.app.Fragment class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "Fragment App" val fragmentManager = supportFragmentManager val fragmentTransaction = fragmentManager.beginTransaction() fragmentTransaction.replace(R.id.frameLayout,FirstFragment()).commit() } fun fragmentMethod() { Toast.makeText(this@MainActivity, "Method called From Fragment", Toast.LENGTH_LONG).show() } }
File: activity_main.xml
–> In activity_main.xml file contain TextView or Framelayout placed inside a Relative Layout.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text=" Developers Dome" android:textAlignment="center" android:textColor="@color/purple_500" android:textSize="32sp" android:textStyle="bold" android:gravity="center_horizontal" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
File: FirstFragment.kt
package com.example.fragmentsbehaviour import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment class FirstFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view: View = inflater.inflate(R.layout.fragment_first, container, false) (activity as MainActivity?)!!.fragmentMethod() return view } }
File: first_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FirstFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginTop="200dp" android:background="@color/teal_200" android:text="Fragment" android:textColor="@color/black" android:textAlignment="center" android:textSize="30sp" /> </FrameLayout>
Pingback: ViewPager Tutorial using Fragment in Android - Developers Dome
Pingback: Activity Lifecycle or Architecture Component - Developers Dome