Mitch
Mitch

Reputation: 121

How to separate data handling from Activity

I have a working Activity (TwalksRouteActivity) that accepts a record id (routeID) from a bundle (passed from a Fragment), pulls the associated record from my repository (routesRepository), and passes an associated value/column (routeName) to my UI. This works fine. However, as I understand best practice (I am learning Android development), the call to my Repository should be in a ViewModel, not an Activity. Is this correct? I have tried but failed to do this myself and would really appreciate some help in how to do this please.

TwalksRouteActivity:

class TwalksRouteActivity() : AppCompatActivity()  {

private lateinit var viewModel: RouteViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    //Log.i("CWM","Called ViewModelProvider")
    //viewModel = ViewModelProvider(this).get(RouteViewModel::class.java)

    var bundle: Bundle? = intent.extras
    var routeID = bundle?.getInt("routeID")

    lifecycleScope.launch (Dispatchers.Main) {
        val database = getDatabase(application)
        val routesRepository = RoutesRepository(database)
        val selectedRoute = routesRepository.getRoute(routeID)
        val routeName = selectedRoute.routeName

        Log.d("CWM", routeName.toString())

        setContentView(R.layout.route_detail)

        val routeName_Text: TextView = findViewById(R.id.routeName_text)
        routeName_Text.text = routeName.toString()
        val routeID_Text: TextView = findViewById(R.id.routeID)
        routeID_Text.text = routeID.toString()
    }
}

}

Upvotes: 0

Views: 83

Answers (1)

You are correct. Best practices include the idea of a ViewModel that handles communications between bussiness logic (your repository) and the activity or fragment which uses or/and dislpays the data. You should check Android Developers ViewModel's official documentation at: ViewModel Overview. Also the guide to app architecture. Check the following image:

App Architecture

As you can see, it describes the data-driven communication flow, and as you said, the ViewModel will call the repository functions that get the data. The ViewModel will then provide the activity with variables and / or functions that can be observed (such as: LiveData), and fire events that the activity will take to make its state changes / data presentation in the UI (this is call reactive pattern).

You should check these Codelabs (free lessons from Google): Incorporate Lifecycle-Aware Components and Android Room with a View - Kotlin (although it mainly covers Room Library, the codelab makes use of ViewModel and Android's best practices recommended by Google). Also, you could check this article: ViewModels and LiveData: Patterns + AntiPatterns.

I could write a lot of code but I think it is beyond the scope of this answer. I'm also learning, and my way was to first understand how these things work and why these things are called "best practices".

Upvotes: 1

Related Questions