admin管理员组文章数量:1417578
I’m developing an application in Jetpack Compose with a main file App.kt
that handles navigation and contains a lower bar (BottomBar) that allows navigation and triggers other shared actions across the entire app. This bar is present at all times, even when navigating to different screens.
The issue is that App.kt
and the bottom navigation bar are always visible. When navigating to Screen A
, Screen B
, or Screen C
, the parent Scaffold
in App.kt
and its bottom bar with navigation buttons etc remain visible.
How to apply the MVVM pattern in this case?
Should the app have an AppViewModel
loaded for App.kt
, managing shared logic across screens (like navigation, dialogs, or shared logic that can be triggered from any screen)?
Additionally, is it okay for each screen (for example, ScreenAViewModel
, ScreenBViewModel
, etc.) to have its own ViewModel, and have them coexist with the AppViewModel
at the same time?
In summary, the app has a fixed navigation bar and other shared functionalities that are always visible on App.kt
, and I want to understand if it's correct for both viewmodels (App and Screen) to coexist at once.
I’m developing an application in Jetpack Compose with a main file App.kt
that handles navigation and contains a lower bar (BottomBar) that allows navigation and triggers other shared actions across the entire app. This bar is present at all times, even when navigating to different screens.
The issue is that App.kt
and the bottom navigation bar are always visible. When navigating to Screen A
, Screen B
, or Screen C
, the parent Scaffold
in App.kt
and its bottom bar with navigation buttons etc remain visible.
How to apply the MVVM pattern in this case?
Should the app have an AppViewModel
loaded for App.kt
, managing shared logic across screens (like navigation, dialogs, or shared logic that can be triggered from any screen)?
Additionally, is it okay for each screen (for example, ScreenAViewModel
, ScreenBViewModel
, etc.) to have its own ViewModel, and have them coexist with the AppViewModel
at the same time?
In summary, the app has a fixed navigation bar and other shared functionalities that are always visible on App.kt
, and I want to understand if it's correct for both viewmodels (App and Screen) to coexist at once.
- Yes, it is fine to have an AppViewModel for shared logic across the app and individual ViewModel for each screen. This aligns with the MVVM pattern and helps anize your code better AppViewModel: Handles shared logic like navigation, dialogs, & shared state/actions accessible from any screen Screen ViewModels: Each screen has its own ViewModel (e.g ScreenAViewModel, ScreenBViewModel) to manage screen-specific state and logic App.kt: Contains the Scaffold with the BottomBar and uses the AppViewModel for shared logic Screens: Each screen use its own ViewModel to manage its state & logic – Tejas Soni Commented Jan 31 at 13:12
- @TejasSoni do you have any official source where they recommend this? – NullPointerException Commented Feb 1 at 8:09
1 Answer
Reset to default 0How to apply the MVVM pattern in this case?
I would like to offer a different implementation angle.
IMO, you should have only one view model for every activity. You could, for example, have several top level Composables that each represent a screen in itself and then switching between them is as trivial as changing a state enum in your view model that says which screen you currently want to show.
But, if you insist on having several view models, i won't stop you... So:
instead of having the App.kt
to show the bottom bar at all times,
create a public global Composable and a BaseViewModel
class that contain the bottom bar functionality logic.
Extend BaseViewModel
with your view models and call your public Composable in your views.
If there's any shared state in the bottom bar you can keep it in your BaseViewModel companion object or another shared object.
I assume that the bottom bar composable interacts with the view model, so it can accept it in the parameters.
本文标签: androidShould App viewmodel and Screen viewmodel coexist at onceStack Overflow
版权声明:本文标题:android - Should App viewmodel and Screen viewmodel coexist at once? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745268981a2650774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论