0

In Compose, while creating Composables like ScreenA (where they would represent the entire screen content), is it a good option to pass in the entire viewmodel as a parameter to the Composable? I mean the values are stored there inside the viewmodel. The same value needs (if), to be accessed from multiple screens like that, how do I read those values. Can I initialize a viewmodel inside the composable, and rest assured that the same instance as the rest of the app will be delivered, or should I just pass in the required value (not viable, I think, since there can be many values over time as development goes through) as a parameter to the Composable?

Using a factory should be a viable solution, really.

Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42
  • Compose have built in solution, `viewModel()`, which perfectly suits it. Check out more in my [answer here](https://stackoverflow.com/a/68806616/3585796) – Phil Dukhov Aug 19 '21 at 05:30
  • Ok so what is the `viewmodel`**s** call that there is. I initialise the viewmodel in my activity as `val vm by viewModels`, but it does not work outside of an activity. Also, is it then not recommended in compose to use a factory? – Richard Onslow Roper Aug 19 '21 at 05:34
  • compose is used to be single activity app, but even if you use many, you can pass your `viewModelStoreOwner` argument to `viewModel` to share between activities. And you can pass your own `factory` to call too – Phil Dukhov Aug 19 '21 at 05:40
  • No I do have only a single activity. I think you didn't read carefully. There are two methods: `viewModel` and `viewModels`. – Richard Onslow Roper Aug 19 '21 at 06:51
  • 1
    First one might be used as `val vm = viewModel()`, only inside composables, and the other, `val vm by viewmodels()`, outside of Composables. – Richard Onslow Roper Aug 19 '21 at 06:52
  • So if I initialise in the activity the one way, and then proceed to go the other way inside the composable, would it still return the same instance? Also, is `Factory` 'discouraged' now? Thanks! – Richard Onslow Roper Aug 19 '21 at 06:53
  • Hey wait. I told in the question that I need to use a particular value from the viewmdoel in multiple composables. Now that value is to be used at multiple places inside each composable, so I cannot use your approach of `viewModel` since it won't allow me to declare a variable on top of the file and use it. I will have to pass down that variable to all the composables. – Richard Onslow Roper Aug 19 '21 at 06:57
  • I actually have to get a specific amount of passing to every element in a screen rendered as a column and it consists of `LazyRow`s so I cannot give the padding to the entire column as a whole. That gives a terrible impression to the `LazyRow`s – Richard Onslow Roper Aug 19 '21 at 06:59
  • I am planning to store that padding value inside the viewmodel, and I want it to be somehow naturally accessible to all the composables inside my screen. – Richard Onslow Roper Aug 19 '21 at 07:00
  • Any other approaches, welcome – Richard Onslow Roper Aug 19 '21 at 07:00
  • read carefully documentation of viewModel. as I said in my [answer](https://stackoverflow.com/a/68806616/3585796), this method returns same object across whole composable tree, you don't need to pass anything by your hands. "since it won't allow me to declare a variable on top of the file and use it" not sure what do you mean here and how composable view modal is different from any other view modal at this point – Phil Dukhov Aug 19 '21 at 07:54

0 Answers0