Android Kotlin – Fragment Lifecycle

fragmentLifecycle

The following picture shows how an activity affects a fragment Callbacks onAttach It is called once when the fragment is attached to the activity onCreate It is called when the fragment is created. Initialize variables, objects etc. onCreateView Here you must inflate the layout file. onActivityCreated Here write anything that requires the activity to exist onStart It is called before it is visible onResume The fragment is visible and has focus onPause It is called when the user is leaving the fragment onStop The fragment has no focus onDestroyView It…

Android Kotlin – Fragment Lifecycle Read More

Android Kotlin – Navigation Add Host Fragment and Connect Destinations with Actions

Screenshot 20181208 124245

On the activity’s layout XML file that you want to add Host Fragment, add the following code. <fragment android:id=”@+id/navHostFragment” android:name=”androidx.navigation.fragment.NavHostFragment” android:layout_width=”match_parent” android:layout_height=”match_parent” app:navGraph=”@navigation/navigation” app:defaultNavHost=”true” /> android:name – class name app:navGraph – which navigation resource to use app:defaultNavHost=”true” – intercepts the system Back button Now, go navigation.xml, switch to Design and add a new Destination by clicking the + symbol at the top To set it the first destination of the navigation click Set Start Destination To create an action, add a second destination, and then click the circle on the first destination and…

Android Kotlin – Navigation Add Host Fragment and Connect Destinations with Actions Read More