首页 > 解决方案 > 将多个共享首选项键添加到鳍状肢共享首选项插件

问题描述

我想知道如何在 Flipper Shared Preferences Viewer Plugin 中显示多个共享首选项键。KEY_FOO, KEY_BAR,KEY_BAZ是共享首选项文件的字符串常量。

就像是

class App: Application() {

     override fun onCreate() {
        super.onCreate()
        setupFlipper()
     }
    
     private fun setupFlipper() {
         if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
            val client = AndroidFlipperClient.getInstance(this)
            client.addPlugin(InspectorFlipperPlugin(this, DescriptorMapping.withDefaults()))

         
            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, KEY_FOO)
            )

            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, KEY_BAR)
            )

            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, KEY_BAZ)
            )

            client.start()
         }
     }

}

标签: androidsharedpreferencesflipperfbflipper

解决方案


检查 SharedPreferencesFlipperPlugin 的构造函数后。存在第二个选项,它采用 SharedPreferencesDescriptor 的列表。

下面的解决方案。

class App: Application() {

     override fun onCreate() {
        super.onCreate()
        setupFlipper()
     }
    
     private fun setupFlipper() {
         if (BuildConfig.DEBUG && FlipperUtils.shouldEnableFlipper(this)) {
            val client = AndroidFlipperClient.getInstance(this)
            client.addPlugin(InspectorFlipperPlugin(this, DescriptorMapping.withDefaults()))

            val keys = mutableListOf(
                KEY_FOO,
                KEY_BAR,
                KEY_BAZ,
            )

            var descriptors: List<SharedPreferencesFlipperPlugin.SharedPreferencesDescriptor> = keys.map {
                SharedPreferencesFlipperPlugin.SharedPreferencesDescriptor(it, MODE_PRIVATE)
            }

            client.addPlugin(
                SharedPreferencesFlipperPlugin(applicationContext, descriptors)
            )

            client.start()
         }
     }

}


推荐阅读