{"id":624,"date":"2025-07-14T01:38:32","date_gmt":"2025-07-14T01:38:32","guid":{"rendered":"https:\/\/boochlin.com\/?p=624"},"modified":"2025-08-27T04:45:10","modified_gmt":"2025-08-27T04:45:10","slug":"fun-gamescreengameviewmodel-gameviewmodel-gameviewmodel-%e9%80%99%e6%a8%a3%e5%af%ab%e6%9c%89%e5%95%a5%e5%95%8f%e9%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=624","title":{"rendered":"fun GameScreen(gameViewModel: GameViewModel = GameViewModel()) \u9019\u6a23\u5beb\u6709\u5565\u554f\u984c\uff1f"},"content":{"rendered":"<h3 class=\"wp-block-heading\">\u76f4\u63a5 new GameViewModel()<\/h3>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-code\"><code>class GameViewModel : ViewModel() {\n    var score by mutableStateOf(0)\n    init { println(\"ViewModel init!\") } \/\/ \u6bcf\u6b21\u91cd\u7d44\u90fd\u6703\u57f7\u884c\n    override fun onCleared() { println(\"ViewModel cleared!\") } \/\/ \u6c38\u9060\u4e0d\u6703\u88ab\u7cfb\u7d71\u547c\u53eb\n}\n\n@Composable\nfun GameScreenWrong(gameViewModel: GameViewModel = GameViewModel()) {\n    \n    \/\/ ... UI \u986f\u793a gameViewModel.score ...\n    Button(onClick = { gameViewModel.incrementScore() }) { \/* ... *\/ }\n}\n<\/code><\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 viewModel() \u51fd\u6578<\/h3>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-code\"><code>@Composable\nfun GameScreenCorrect(\n    gameViewModel: GameViewModel = viewModel() \/\/ &lt;-- \u6b63\u78ba\u65b9\u5f0f\uff01\n) {\n    \/\/ ... UI \u986f\u793a gameViewModel.score ...\n    Button(onClick = { gameViewModel.incrementScore() }) { \/* ... *\/ }\n}\n<\/code><\/pre>\n<p>viewModel() \u662f\u4e00\u500b Composable \u51fd\u6578\uff0c\u5b83\u6703\uff1a<\/p>\n<ol start=\"1\">\n<li><strong>\u5c0b\u627e ViewModelStoreOwner\uff1a<\/strong> \u5411\u4e0a\u67e5\u627e\u6700\u8fd1\u7684 Activity\u3001Fragment \u6216 NavBackStackEntry\u3002<\/li>\n<li><strong>\u5229\u7528 ViewModelProvider\uff1a<\/strong> \u900f\u904e ViewModelProvider <strong>\u53d6\u5f97\u6216\u5275\u5efa<\/strong> ViewModel \u5be6\u4f8b\u3002\u5982\u679c\u5df2\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u73fe\u6709\u5be6\u4f8b\uff1b\u5982\u679c\u6c92\u6709\uff0c\u5247\u5275\u5efa\u4e26\u5b58\u5132\uff0c\u78ba\u4fdd\u8207\u5176 ViewModelStoreOwner \u751f\u547d\u9031\u671f\u7d81\u5b9a\u3002<\/li>\n<li><strong>\u7d81\u5b9a\u751f\u547d\u9031\u671f\uff1a<\/strong> \u9019\u6a23\u7372\u53d6\u7684 ViewModel \u6703\u5728 ViewModelStoreOwner \u92b7\u6bc0\u6642\u7531\u6846\u67b6\u81ea\u52d5\u6e05\u7406\uff0c\u4e26\u547c\u53eb onCleared()\u3002<\/li>\n<\/ol>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2 class=\"wp-block-heading\">\u4f60\u53ef\u80fd\u6703\u554f GameViewModel \u660e\u660e\u5c31\u662f\u7e7c\u627f ViewModel<\/h2>\n<p>\u7576\u4f60\u8b93 GameViewModel \u7e7c\u627f ViewModel \u6642\uff0c\u4f60\u662f\u5728\u5411 Android \u6846\u67b6\u5ba3\u544a\uff1a\u300c\u6211\u9019\u500b GameViewModel \u662f\u4e00\u500b\u7279\u6b8a\u7684\u985e\u5225\uff0c\u5b83\u9700\u8981\u88ab\u6846\u67b6\u7684\u751f\u547d\u9031\u671f\u6a5f\u5236\u6240\u7ba1\u7406\u3002\u300d<\/p>\n<p>\u9019\u500b\u7e7c\u627f\u95dc\u4fc2\u672c\u8eab\u4e26\u6c92\u6709\u8b93\u4f60\u7684 GameViewModel \u7372\u5f97\u9b54\u6cd5\uff0c\u800c\u662f\u544a\u8a34\u6846\u67b6\uff1a\u300c\u6211\u662f\u4e00\u500b <strong>ViewModel<\/strong>\uff0c\u8acb\u4f60\u7528\u8655\u7406 <strong>ViewModel<\/strong> \u7684\u65b9\u5f0f\u4f86\u8655\u7406\u6211\u3002\u300d<\/p>\n<p>\u60f3\u50cf\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>ViewModel \u662f\u4e00\u500b\u4ecb\u9762\u6216\u62bd\u8c61\u985e\u5225\uff0c\u5b9a\u7fa9\u4e86\u300c\u5982\u4f55\u88ab\u6846\u67b6\u7ba1\u7406\u300d\u7684\u5408\u7d04\u3002<\/strong> \u5b83\u5305\u542b\u50cf onCleared() \u9019\u6a23\u7684\u65b9\u6cd5\uff0c\u9019\u4e9b\u65b9\u6cd5\u53ea\u6709\u5728\u6846\u67b6\u7ba1\u7406\u5b83\u6642\u624d\u6703\u6709\u610f\u7fa9\u3002<\/li>\n<li><strong>\u4f60\u7e7c\u627f ViewModel\uff0c\u5c31\u50cf\u662f\u4f60\u7684 GameViewModel \u7a7f\u4e0a\u4e86\u5beb\u8457\u300c\u6211\u662f\u751f\u547d\u9031\u671f\u7ba1\u7406\u7269\u4ef6\u300d\u7684\u5236\u670d\u3002<\/strong><\/li>\n<\/ul>\n<p>\u7136\u800c\uff0c\u55ae\u7d14\u7a7f\u4e0a\u5236\u670d\u4e26\u4e0d\u4ee3\u8868\u4f60\u5c31\u81ea\u52d5\u88ab\u7ba1\u7406\u4e86\u3002\u4f60\u9084\u9700\u8981<strong>\u88ab\u300c\u8a3b\u518a\u300d\u5230\u7ba1\u7406\u7cfb\u7d71\u4e2d<\/strong>\u3002<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h2 class=\"wp-block-heading\">\u554f\u984c\u7684\u6839\u6e90\uff1a\u6c92\u6709\u88ab\u300c\u8a3b\u518a\u300d\u5230 ViewModelStore<\/h2>\n<p>GameViewModel \u7e7c\u627f ViewModel \u56fa\u7136\u662f\u7b2c\u4e00\u6b65\uff0c\u4f46\u5149\u662f\u7e7c\u627f\uff0c\u4f60\u7684 GameViewModel \u5be6\u4f8b\u4e26\u4e0d\u6703\u81ea\u52d5\u88ab Android \u6846\u67b6\u7684 ViewModelStore \u6240\u6301\u6709\u548c\u7ba1\u7406\u3002<\/p>\n<p>ViewModelStore \u662f Android \u6846\u67b6\u4e2d\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u7d44\u4ef6\uff0c\u5b83\u8ca0\u8cac\uff1a<\/p>\n<ol start=\"1\">\n<li><strong>\u5132\u5b58 ViewModel \u5be6\u4f8b\uff1a<\/strong> \u5728\u914d\u7f6e\u8b8a\u66f4\uff08\u4f8b\u5982\u87a2\u5e55\u65cb\u8f49\uff09\u6642\uff0c\u4fdd\u7559 ViewModel \u5be6\u4f8b\uff0c\u4f7f\u5176\u4e0d\u88ab\u92b7\u6bc0\u3002<\/li>\n<li><strong>\u7d81\u5b9a\u751f\u547d\u9031\u671f\uff1a<\/strong> \u76e3\u807d\u5176\u5bbf\u4e3b\uff08\u5982 Activity \u6216 Fragment\uff09\u7684\u751f\u547d\u9031\u671f\uff0c\u7576\u5bbf\u4e3b\u6c38\u4e45\u92b7\u6bc0\u6642\uff0c\u6703\u547c\u53eb\u5176\u4e2d\u6240\u6709 ViewModel \u7684 onCleared() \u65b9\u6cd5\u4f86\u91cb\u653e\u8cc7\u6e90\u3002<\/li>\n<\/ol>\n<h3 class=\"wp-block-heading\">1. \u4f60\u624b\u52d5 new GameViewModel()\uff1a<\/h3>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-code\"><code>class GameViewModel : ViewModel() { \/* ... *\/ }\n\n@Composable\nfun GameScreenWrong() {\n    val gameViewModel = GameViewModel() \/\/ &lt;-- \u76f4\u63a5 new\n    \/\/ ...\n}\n<\/code><\/pre>\n<ul>\n<li><strong>\u7e7c\u627f\u4e86 ViewModel\uff1a<\/strong> GameViewModel \u78ba\u5be6\u7e7c\u627f\u4e86 ViewModel\uff0c\u5b83\u6709 onCleared() \u65b9\u6cd5\uff0c\u4e5f\u80fd\u4f7f\u7528 viewModelScope\u3002<\/li>\n<li><strong>\u4f46\u6c92\u6709\u88ab\u300c\u8a3b\u518a\u300d\uff1a<\/strong> \u4f60\u76f4\u63a5 new \u4e86\u4e00\u500b GameViewModel \u7269\u4ef6\u3002\u9019\u500b\u7269\u4ef6\u662f\u7368\u7acb\u5b58\u5728\u7684\uff0c\u5b83\u4e26\u6c92\u6709\u88ab\u4efb\u4f55 ViewModelStore \u6240\u77e5\u9053\u6216\u6301\u6709\u3002\u5b83\u5c31\u50cf\u4f60\u79c1\u4eba\u5275\u5efa\u7684\u4e00\u500b\u300c\u7a7f\u8457\u5236\u670d\u7684\u7269\u4ef6\u300d\uff0c\u4f46\u4e26\u6c92\u6709\u9032\u5165\u90a3\u500b\u300c\u9700\u8981\u88ab\u7ba1\u7406\u7684\u968a\u4f0d\u300d\u4e2d\u3002<\/li>\n<li><strong>\u7d50\u679c\uff1a<\/strong>\n<ul>\n<li>\u7576 GameScreenWrong \u91cd\u7d44\u6642\uff0c\u5b83\u6703\u518d\u6b21 new \u4e00\u500b\u65b0\u7684 GameViewModel\uff0c\u56e0\u70ba\u820a\u7684\u5be6\u4f8b\u6c92\u6709\u88ab ViewModelStore \u6301\u6709\u548c\u8fd4\u56de\uff0c\u6240\u4ee5\u72c0\u614b\u6703\u907a\u5931\u3002<\/li>\n<li>\u7576 GameScreenWrong \u6240\u5728\u7684 Activity \u6216 Fragment \u92b7\u6bc0\u6642\uff0cViewModelStore \u4e0d\u77e5\u9053\u4f60\u9019\u500b\u624b\u52d5 new \u7684 GameViewModel \u5b58\u5728\uff0c\u6240\u4ee5\u6c38\u9060\u4e0d\u6703\u547c\u53eb\u5b83\u7684 onCleared() \u65b9\u6cd5\uff0c\u5c0e\u81f4\u8a18\u61b6\u9ad4\u6d29\u6f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 viewModel() \u51fd\u6578\uff1a<\/h3>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-code\"><code>@Composable\nfun GameScreenCorrect(\n    gameViewModel: GameViewModel = viewModel() \/\/ &lt;-- \u900f\u904e viewModel()\n) {\n    \/\/ ...\n}\n<\/code><\/pre>\n<ul>\n<li><strong>\u7e7c\u627f\u4e86 ViewModel\uff1a<\/strong> GameViewModel \u4f9d\u7136\u7e7c\u627f ViewModel\u3002<\/li>\n<li><strong>\u88ab viewModel() \u300c\u8a3b\u518a\u300d\u4e86\uff1a<\/strong>viewModel() \u9019\u500b Compose \u51fd\u6578\u7684\u5167\u90e8\u6703\uff1a\n<ol start=\"1\">\n<li>\u627e\u5230\u6700\u8fd1\u7684 <strong>ViewModelStoreOwner<\/strong> (\u901a\u5e38\u662f Activity \u6216 Fragment)\u3002<\/li>\n<li>\u900f\u904e <strong>ViewModelProvider<\/strong> (\u5b83\u6703\u8a2a\u554f ViewModelStoreOwner \u5167\u90e8\u7684 ViewModelStore)\uff0c\u53bb\u7372\u53d6\u6216\u5275\u5efa GameViewModel \u5be6\u4f8b\u3002<\/li>\n<li>\u5982\u679c\u5275\u5efa\u4e86\u65b0\u7684\u5be6\u4f8b\uff0c\u5b83\u6703<strong>\u5c07\u9019\u500b\u5be6\u4f8b\u300c\u8a3b\u518a\u300d\u5230\u5c0d\u61c9\u7684 ViewModelStore \u4e2d<\/strong>\u3002<\/li>\n<\/ol>\n<\/li>\n<li><strong>\u7d50\u679c\uff1a<\/strong>\n<ul>\n<li>\u56e0\u70ba\u88ab ViewModelStore \u6301\u6709\uff0c\u6240\u4ee5\u5728\u91cd\u7d44\u6216\u914d\u7f6e\u66f4\u6539\u6642\uff0cviewModel() \u6703\u5f9e ViewModelStore \u4e2d\u8fd4\u56de<strong>\u540c\u4e00\u500b<\/strong> GameViewModel \u5be6\u4f8b\uff0c\u72c0\u614b\u5f97\u4ee5\u4fdd\u7559\u3002<\/li>\n<li>\u7576 Activity \u6216 Fragment \u6c38\u4e45\u92b7\u6bc0\u6642\uff0cViewModelStore \u6703\u81ea\u52d5\u547c\u53eb\u5176\u5167\u90e8\u6240\u6709 ViewModel \u7684 onCleared() \u65b9\u6cd5\uff0c\u78ba\u4fdd\u8cc7\u6e90\u88ab\u6b63\u78ba\u91cb\u653e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>val gameViewModel = viewModel()\nval gameViewModel = ViewModelProvider(viewModelStoreOwner, factory).get(GameViewModel::class.java)\n\n\u5176\u5be6\u5c31\u662f\u9019\u6a23\u5566\u4e0a\u9762\u5169\u884c\u662f\u7b49\u50f9\u7684<\/code><\/pre>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76f4\u63a5 new GameViewModel() Kotlin class GameViewModel : ViewModel() { var score by mutableStateOf(0) ini&#8230;<\/p>\n","protected":false},"author":1,"featured_media":842,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/624"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=624"}],"version-history":[{"count":16,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":845,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/624\/revisions\/845"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/842"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}