https://kotlinlang.org logo
#compose
Title
# compose
c

codeslubber

01/11/2021, 9:07 PM
Is it possible to navigate between activities? and have the bottom bar work? seems like the answer is no (for now?)…
i

Ian Lake

01/11/2021, 9:15 PM
You mean have each bottom nav item open a brand new activity? That hasn't been a recommended pattern...well, ever lol
😁 1
c

codeslubber

01/11/2021, 9:22 PM
So one activity is a user profile and the other is a FAQ and those should be fragments that share a Viewmodel? that sounds great!
i

Ian Lake

01/11/2021, 9:32 PM
Or, since you're in the #compose channel, composables, but yes
c

codeslubber

01/11/2021, 9:34 PM
Yeah that’s sad (I was joking about it sounding great), I was so thrilled when Viewmodels arrived and the Blueprint stuff, but I guess the right way to do this is just put the various unrelated things in the Drawer. Thanks @Ian Lake
So I reread the docs, but kept searching and found this SO question (https://stackoverflow.com/questions/61038532/view-model-can-be-heavy-if-we-use-single-activity-based-architecture) that @Ian Lake answered, which gave me a surprise: Viewmodels can be scoped to Fragments! the documentation does not show how that’s done, though? in the docs they show a Master(sic)/Detail that shares data between the fragments (which is cool)…
i

Ian Lake

01/11/2021, 10:00 PM
You can scope ViewModels to activities, fragments, navigation graphs or composables in a Navigation Compose graph, yup
The fragment docs talk about using ViewModels in Fragments, FWIW: https://developer.android.com/guide/fragments/saving-state#nonconfig
In a Compose world, you use
= viewModel()
- https://developer.android.com/jetpack/compose/interop#viewmodel
Which, if you're using Navigation Compose (https://developer.android.com/jetpack/compose/navigation), will automatically scope your
ViewModel
to the current destination if used within the
NavHost
c

codeslubber

01/11/2021, 10:14 PM
so then to summarize, my mistake was to start with the Viewmodels page in the docs 😉 … this is great!! thanks!
i

Ian Lake

01/11/2021, 10:15 PM
Yeah, ViewModels are one of the lowest level building blocks - lower level than fragments or Navigation. You'd have to go to the docs on the higher level components to find out how they expose those lower level building blocks
c

codeslubber

01/11/2021, 10:17 PM
Well I would suggest that the Viewmodels page mention that they can be scoped to all those things. When they were first introduced they were scoped only to Activities.
But thanks, on the whole samples and docs in android are amazing…
i

Ian Lake

01/11/2021, 10:25 PM
You mean when they were first introduced in the docs? Because ViewModels themselves had support for fragments on day 1 (way back before AndroidX was even a thing)
Do you mind filing a documentation bug for the ViewModel page? I think there could at least be a few more 'learn more' links at the bottom pointing to the other pages. https://issuetracker.google.com/issues/new?component=192697
c

codeslubber

01/11/2021, 10:42 PM
Yeah I am happy to do that.
On the first issue, I am not saying they did not have support for fragments, I am saying they were scoped to the activity so the share one viewmodel between one or more fragments, sure that worked, a different viewmodel for different fragments? I don’t think so.
i

Ian Lake

01/11/2021, 11:10 PM
The
ViewModelProviders.of(Fragment)
method which allowed fragment scoped ViewModels is in the very first
android.arch.lifecycle:lifecycle-extensions:1.0.0-alpha01
release 🙂
c

codeslubber

01/11/2021, 11:14 PM
I was wrong.. perfect 🙂
That’s interesting, but since that was before the Single Activity reborn movement, not sure how germane…
5 Views