ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Member-only story

Load Initial Data When Screen Appears

Caner Kaşeler
ProAndroidDev
Published in
7 min read3 days ago

Many mobile applications need to fetch data to display on the screen when a screen is shown. Additionally, when the application configuration changes, it may not always be necessary to send new API requests, read from the database, etc.

In Android application development, we have several options for handling this. In this article, I will discuss some of them along with my personal preference.

I no longer use Fragments in my projects. However, these days, the likelihood of encountering both Fragments and Jetpack Compose together is higher than encountering Fragments alone. Therefore, I have included both in this article. Lastly, I will provide an example of fetching data, but there may be other possible approaches as well.

Article image made by Caner Kaşeler.

Table of contents:

  1. Example Project Structure 🏗️
  2. Possible Ways 🛣️
  3. My Personal Preference 👀
  4. Last Words 📝

1) Example Project Structure 🏗️

Firstly, let’s have a quick look at the project structure. I used Hilt for dependency injection in this article.

Application

It is basic application class with Hilt annotation.

package com.canerkaseler

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App: Application()

MainActivity

Project has single fragment which is HomeFragment:

package com.canerkaseler

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.add
import androidx.fragment.app.commit
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Caner Kaşeler

Senior Android Developer at Tangerine Bank 🍊 Write Medium articles and GitHub repositories ⌨️ Share posts about Compose and Kotlin Multiplatform 🖼️

Responses (1)

Write a response

Instead of inflating layout in onCreateView and then setContent on ComposeView declared in a XML, we can use `content {}` function from `androidx.fragment:fragment-compose`:

--