Skip to content

Instantly share code, notes, and snippets.

@FareesHussain
Last active July 31, 2020 18:01
Show Gist options
  • Save FareesHussain/75edfe6207f56d672614ad851eb89742 to your computer and use it in GitHub Desktop.
Save FareesHussain/75edfe6207f56d672614ad851eb89742 to your computer and use it in GitHub Desktop.
all about fragments

Basics

  • creating a fragment

demo - #1

MainFragment.kt

class MainFragment:Fragment(R.layout.fragment_main){
	override onViewCreated(view: View,savedInstancestate:Bundle?){
		super.onViewCreated(view,savedInstanceState)
	}
}

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	
</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

<...ConstraintLayout
	..
	>
	<fragment
		android:name = ".MainFragment"
		..
		/>
</..ConstraintLayout>	

fragment lifecycle

Fragment is Created

----------Created-----------

  1. Fragment is added
  2. onAttach()
  3. onCreate()
  4. onCreateView()
  5. onActivityCreated()

----------Started-----------

  1. onStart()

----------Resumed-----------

  1. onResume()

Fragment is active

----------Paused-----------

  1. onPause()

----------Stopped-----------

  1. onStop()

----------Destroyed-----------

  1. onDestroyView()
  2. onDestroy()
  3. onDetach()

Fragment is destroyed

Fragments

  • creating a fragment
  • using in jetpack navigations
  • view pager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment