{"id":1581,"date":"2022-12-08T11:02:21","date_gmt":"2022-12-08T03:02:21","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1581"},"modified":"2023-01-13T11:30:47","modified_gmt":"2023-01-13T03:30:47","slug":"%e3%80%90kotlin%e3%80%91android-stateflow-%e6%9e%b6%e6%a7%8b-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%e3%80%90kotlin%e3%80%91android-stateflow-%e6%9e%b6%e6%a7%8b-%e7%af%84%e4%be%8b\/","title":{"rendered":"\u3010Kotlin\u3011StateFlow \u72c0\u614b\u8cc7\u6599\u6d41 \u7bc4\u4f8b"},"content":{"rendered":"<h1>\u3010Kotlin\u3011StateFlow \u72c0\u614b\u8cc7\u6599\u6d41 \u7bc4\u4f8b<\/h1>\n<h4>\u672c\u6587\u5c07\u4ecb\u7d39Android StateFlow\uff0c\u5b83\u662f\u4e00\u500b\u65b0\u51fa\u73fe\u7684API\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5be6\u73fe\u8de8\u81ea\u5b9a\u7fa9\u7dda\u7a0b\u7684\u72c0\u614b\u7ba1\u7406\uff0c\u4ee5\u53ca\u5728\u5e94\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\u5f37\u5927\u800c\u53ef\u9760\u7684\u4e00\u81f4\u6027\u3002<\/h4>\n<h4>\u5b83\u652f\u6301Android\u7684\u8f49\u5834\u72c0\u614b\uff0c\u4e26\u53ef\u4ee5\u5728\u72c0\u614b\u66f4\u6539\u6642\u767c\u51fa\u4e8b\u4ef6\u3002<\/h4>\n<h4>StateFlow\u53ef\u4ee5\u5728\u591a\u500b\u7dda\u7a0b\u4e4b\u9593\u5171\u4eab\uff0c\u800c\u4e0d\u6703\u72a7\u7272\u6027\u80fd\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528\u540c\u4e00\u7247\u72c0\u614b\u3002<\/h4>\n<h4>\u5728\u672c\u6587\u4e2d\uff0c\u5c07\u6df1\u5165\u8a0e\u8ad6StateFlow\u7684\u7528\u9014\uff0c\u5176\u4ed6Android\u6280\u8853\u4ee5\u53ca\u5982\u4f55\u512a\u5316\u72c0\u614b\u7ba1\u7406\u7cfb\u7d71\uff0c\u4f7f\u5176\u66f4\u6709\u6548\u4e26\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u6027\u80fd\u3002<\/h4>\n<h4>\u6700\u5f8c\uff0c\u5c07\u63a2\u7d22StateFlow API\u63d0\u4f9b\u7684\u984d\u5916\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u6bcf\u500b\u65b0\u9805\u76ee\u4e2d\u4f7f\u7528\u5b83\uff0c\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u4f60\u7684\u72b6\u614b\u7ba1\u7406\u7cfb\u7d71\u3002<\/h4>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">Fragment KTX \u5c0e\u5165<\/a><\/li>\n<li><a href=\"#b\">StateFlow Layout<\/a><\/li>\n<li><a href=\"#c\">StateFlow MainActivity<\/a><\/li>\n<li><a href=\"#d\">StateFlow MainViewModel<\/a><\/li>\n<li><a href=\"#e\">StateFlow PersonState<\/a><\/li>\n<li><a href=\"#f\">Developer Documents StateFlow<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.Fragment KTX \u5c0e\u5165<\/h4>\n<h5>build.gradle<\/h5>\n<pre><code class=\"language-groovy line-numbers\">dependencies {\n    \/\/lifecycleScope\u3001viewModelScope\u3001by viewModels()\n    implementation 'androidx.fragment:fragment-ktx:1.5.5'\n}\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.StateFlow Layout<\/h4>\n<h5>activity_main.xml<\/h5>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/name\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:textSize=\"50sp\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.StateFlow MainActivity<\/h4>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private lateinit var binding: ActivityMainBinding\n    private val viewModel: MainViewModel by viewModels()\n\n    @SuppressLint(\"SetTextI18n\")\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        binding = ActivityMainBinding.inflate(layoutInflater)\n        setContentView(binding.root)\n\n        lifecycleScope.launch {\n            repeatOnLifecycle(Lifecycle.State.STARTED) {\n                viewModel.personState.collect {\n                    binding.name.apply {\n                        text = \"<span class=\"katex math inline\">{it.name}\\n<\/span>{it.age}\"\n                        gravity = Gravity.CENTER\n                    }\n                }\n            }\n        }\n\n        binding.name.setOnClickListener {\n            viewModel.refreshData()\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.StateFlow MainViewModel<\/h4>\n<h5>MainViewModel.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainViewModel : ViewModel() {\n\n    private val _personState = MutableStateFlow(PersonState())\n    val personState = _personState.asStateFlow()\n\n    fun refreshData() {\n        viewModelScope.launch {\n            _personState.update {\n                if (it.name == \"wade\") {\n                    it.copy(name = \"fly\", age = 20)\n                } else {\n                    it.copy(name = \"wade\", age = 25)\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.StateFlow PersonState<\/h4>\n<h5>PersonState.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">data class PersonState(\n    val name: String = \"fly\",\n    val age: Int = 20\n)\n<\/code><\/pre>\n<p><a id=\"f\"><\/a><\/p>\n<h4>6.Developer Documents StateFlow<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.android.com\/kotlin\/flow\/stateflow-and-sharedflow\" title=\"Open in Documents StateFlow\" target=\"_blank\" rel=\"noopener\">Open in Documents StateFlow<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u3010Kotlin\u3011StateFlow \u72c0\u614b\u8cc7\u6599\u6d41 \u7bc4\u4f8b \u672c\u6587\u5c07\u4ecb\u7d39Android StateFlow\uff0c\u5b83\u662f\u4e00\u500b\u65b0 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[6],"tags":[13,15,217],"class_list":["post-1581","post","type-post","status-publish","format-standard","hentry","category-kotlin","tag-android","tag-kotlin","tag-stateflow"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1581"}],"version-history":[{"count":8,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1581\/revisions"}],"predecessor-version":[{"id":1619,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1581\/revisions\/1619"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}