3

So I'm checking this official Compose tutorial https://developer.android.com/courses/pathways/compose

There is Jetpack Compose Navigation

https://developer.android.com/codelabs/jetpack-compose-navigation

So basically in that sample there is just one activity, no fragments and composables (implements 3 screens) are used for navigation graph instead of fragments

This sample is also really bad because it doesn't have ViewModels at all...

So basically how would a real app would look right now?

A single Activity with a single ViewModel and a lot of screens made in composables which use the same ViewModel (which of course is really bad)? Or I guess we should add a separate ViewModel inside of each start composable function of a specific screen instead of Activity (which could have only one single ViewModel - one lifecycle)

enter image description here

So basically we don't need fragments any more nowadays?

user924
  • 8,146
  • 7
  • 57
  • 139
  • This doesn't seem to be a question. But here are a few pointers - no we don't need fragments any more. ViewModel's live in the BackStackEntry and are garbage collected when they go out of scope. Also in compose there is a new idea in the community where you don't need viewmodels any more - this is possible as Compose funciton are configuration change aware. – clamentjohn Dec 17 '21 at 12:01
  • @clamentjohn how I start/stop camera in compose screen then? https://stackoverflow.com/questions/70391108/lifecycle-of-android-components-resources-api-such-as-camera-api-in-single-jet?noredirect=1&lq=1 – user924 Dec 17 '21 at 13:27
  • You can use a [LocalLifecycleOwner](https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/package-summary#LocalLifecycleOwner()) to get the current lifecycle. This [example](https://gist.githubusercontent.com/pengj/17e172110dee1bd1dc074d87b3eb90f3/raw/fce0e50af023b775baa9968e432a85273431af88/SimpleCameraPreview.kt) seem to have an example solution – clamentjohn Dec 17 '21 at 14:24
  • check out official [compose-samples](https://github.com/android/compose-samples), it has some projects with view models and navigation – Phil Dukhov Dec 18 '21 at 01:57

0 Answers0