Fragments, Navigation, Widgets, and Logging


  • Fragment represents a reusable portion of your app’s UI.
  • A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events.
  • Fragments cannot live on their own–they must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to, the host’s view hierarchy.

Fragment lifecycle

  • Each Fragment instance has its own lifecycle.
  • When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.
  • To manage lifecycle, Fragment implements LifecycleOwner, exposing a Lifecycle object that you can access through the getLifecycle() method.
Fragment Lifecyclestates and their relation both the fragment’s lifecycle callbacks and the fragment’s view Lifecycle.

Share data between fragments

  • These fragments can share a ViewModel using their activity scope to handle this communication

TaskListFragment – Start Fragment
 * By activityViewModels is a property delegate to get a reference to the ViewModel scoped to
 * its Activity. So, our shared ViewModel will survive across Fragment recreation.
private val taskViewModel: TaskViewModel by activityViewModels {


  • Navigation refers to the interactions that allow users to navigate across, into, and back out from the different pieces of content within your app.
  • Android Jetpack’s Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer.
  • The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.


  • Menus are a common user interface component in many types of applications.
  • To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.


  • Useful widgets for the implementation

Date Picker


  • Spinners provide a quick way to select one value from a set. In the default state, a spinner shows it’s currently selected value.
  • Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


  • Logging too much information will blow up your Android monitor
  • OkHttp’s logging interceptor has four log levels: NONEBASICHEADERSBODY.
object ServiceBuilder {
vargson: Gson = GsonBuilder()

// If you need to check your object ServiceBuilder {
    var gson: Gson = GsonBuilder()

    // If you need to check your request change the Level
    var loggingInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().setLevel(

    private val client =

    private val retrofit = Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL) // change this IP for testing by your actual machine IP

    fun <T> buildService(service: Class<T>): T {
        return retrofit.create(service)
} change the Level
varloggingInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().setLevel(

private valclient=

private valretrofit= Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL)// change this IP for testing by your actual machine IP

fun<T> buildService(service: Class<T>): T {

Github Repository