Fragments, Navigation, Widgets, and Logging

Fragments

  • 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 {
    TaskViewModelFactory()
}

Navigation

  • 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.

Menu

  • 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.

Widgets

  • Useful widgets for the implementation

Date Picker

Spinners

  • 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

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

// If you need to check your object ServiceBuilder {
    var gson: Gson = GsonBuilder()
        .setDateFormat(BuildConfig.DATE_FORMAT)
        .create()

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

    private val client =
        OkHttpClient.Builder().addInterceptor(loggingInterceptor)
            .addInterceptor(AuthorizationInterceptor()).build()

    private val retrofit = Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL) // change this IP for testing by your actual machine IP
        .addConverterFactory(GsonConverterFactory.create(gson))
        .client(client)
        .build()

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

private valclient=
        OkHttpClient.Builder().addInterceptor(loggingInterceptor)
            .addInterceptor(AuthorizationInterceptor()).build()

private valretrofit= Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_URL)// change this IP for testing by your actual machine IP
.addConverterFactory(GsonConverterFactory.create(gson))
        .client(client)
        .build()

fun<T> buildService(service: Class<T>): T {
returnretrofit.create(service)
    }
}

Github Repository