Posts

Showing posts with the label Fragment

FragmentStatePagerAdapter in Android

Image
Overview FragmentStatePagerAdapter is used to communicate between fragments. FragmentStatePagerAdapter is deprecated by the way. Example of FragmentStatePagerAdapter <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" tools :context =".MainActivity" > <androidx.viewpager.widget.ViewPager android :id ="@+id/viewpager" android :layout_width ="match_parent" android :layout_height ="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout> class FragmentAdapter( private val fragmentManager : FragmentManager) : FragmentStatePagerAdapter(fragmentManager...

Fragment in Android

Image
Overview Fragment is also called sub-activity. It is an application component which is dependent on Fragment. Following is the Fragment lifecycle. Activity with Fragment rotated Steps to follow Enable view binding in build.gradle (Module) file to use ActivityMainBinding. buildFeatures { viewBinding true } Create blank fragments inside an Android project. It will create a Java/Kotlin file and the associated xml file.  Extend the Java/Kotlin file with Fragment class. (When you create a new Fragment on Android Studio, the studio automatically creates basic boilerplate codes for you). class FragmentA : Fragment() { private lateinit var communicator : Communicator override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup? , savedInstanceState : Bundle? ) : View? { communicator = activity as Communicator val view = inflater.inflate(R.layout. fragment_a , container , false ) view.btnSend.setOnClickListener { ...