gitmyhub

Jetpack-MVVM-Best-Practice

Java ★ 8.9k updated 2y ago

难得一见 Jetpack MVVM 最佳实践。在 "以简驭繁" 代码中,对 "视图控制器" 乃至 "标准化开发模式" 形成正确、深入理解。

A sample Android music player app that shows Java developers how to correctly use Google Jetpack components like ViewModel, LiveData, and Navigation without common mistakes.

JavaAndroidJetpackViewModelLiveDataNavigationDataBindingsetup: moderatecomplexity 3/5

This is an Android sample application that demonstrates how to use Google's Jetpack MVVM architecture correctly and without common mistakes. The README is written in Chinese, and the project is aimed at Android developers who want a clear, working example of how Jetpack components fit together, rather than yet another article that pastes disconnected code snippets.

Jetpack is a collection of Android libraries from Google covering things like ViewModel (which stores screen state across configuration changes), LiveData (which notifies the UI when data changes), Navigation (which manages screen transitions), and DataBinding (which connects layout files directly to data). The project shows how to wire these pieces together in a way that avoids memory leaks and unpredictable errors, particularly when switching between portrait and landscape layouts or when sending messages between different screens.

The sample app takes the form of a music player, similar in appearance to Tencent's lighter music app. It demonstrates portrait-to-landscape layout switching with no unexpected errors, a unified event distribution system using LiveData and shared ViewModels, and a layered architecture where the UI layer talks to the ViewModel layer, which in turn talks to a DataRepository, with no reverse dependencies. The project avoids dependency injection tools like Dagger and explicitly states the goal is readable, maintainable code rather than clever tricks.

The code is written entirely in Java and is licensed under the Apache License 2.0. According to a survey in the README, the architecture patterns shown here have been referenced or adopted by software teams at Tencent Music, NetEase Cloud Music, TCL, BMW, and several other companies. The app is available to download and try directly from app stores.

Where it fits