android - Should App viewmodel and Screen viewmodel coexist at once? - Stack Overflow

admin2025-04-22  0

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.

Share Improve this question edited Feb 3 at 22:23 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 31 at 10:22 NullPointerExceptionNullPointerException 37.9k80 gold badges231 silver badges405 bronze badges 2
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

How 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745268983a293458.html

最新回复(0)