Member-only story
Load Initial Data When Screen Appears
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.

Table of contents:
- Example Project Structure 🏗️
- Possible Ways 🛣️
- My Personal Preference 👀
- 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())…