-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd89720
commit 25b2199
Showing
11 changed files
with
358 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,45 @@ | ||
快速在Activity/Fragment中传递和获取参数 | ||
快速在Activity/Fragment中传递和接受参数 | ||
|
||
可以解决Java中存在的默认值问题. <br> | ||
譬如Intent中传递一个Int值. 即使你没有传递该Int值也是0(这在Kotlin中是错误的行为). 没有传递值应当默认为null | ||
!!! failure "默认值" | ||
基础类型默认值为null(未指定默认值情况下), 请注意非可空类型读到null会崩溃 | ||
|
||
> 前一章介绍的`SerializeHook`并不支持bundle()等方法 | ||
|
||
### 传递参数 | ||
|
||
=== "打开Activity" | ||
在界面A传递 | ||
|
||
=== "Activity" | ||
```kotlin | ||
openActivity<TestActivity>( | ||
"parcelize" to ModelParcelable(), | ||
"name" to "吴彦祖", | ||
"name" to "名称", | ||
"age" to 34 | ||
) | ||
``` | ||
|
||
=== "打开Fragment" | ||
=== "Fragment" | ||
```kotlin | ||
MyFragment().withArguments( | ||
"parcelize" to ModelParcelable(), | ||
"name" to "吴彦祖", | ||
"name" to "名称", | ||
"age" to 34 | ||
) | ||
``` | ||
|
||
### 读取参数 | ||
|
||
在界面B读取 | ||
|
||
```kotlin | ||
private val parcelize: ModelParcelable by bundle() | ||
private val name:String by bundle() | ||
private val age:Int by bundle() | ||
``` | ||
|
||
1. 允许字段为任何访问权限(例如private/public). 这点比ARouter路由框架的自动注入字段优秀的多(不推荐使用ARouter) | ||
1. 不使用`openActivity`等Serialize框架的函数打开的界面也可以读取参数 | ||
1. 可以是成员属性也可以是局部变量 | ||
1. 允许字段为任何访问权限(例如private/public) | ||
1. 不使用`openActivity`等函数也可以接受数据 | ||
|
||
|
||
## 泛型问题 | ||
## 集合泛型 | ||
|
||
假设写入的`List<String>`, 读取的是`List<Int>`, 也会返回一个正常的集合对象, 但是`get[]`时候会导致抛出`ClassCastException`类型转换异常(官方也是如此) | ||
写入`List<String>`, 读取就一定是`List<String>`, 否则集合`get[]`时会抛出`ClassCastException` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
## 单例配置 | ||
|
||
为类添加注解`@SerializeConfig`可指定其所有`serialXX()`字段的[MMKV实例配置](https://github.com/Tencent/MMKV/wiki/android_advance) | ||
|
||
```kotlin | ||
@SerializeConfig(mmapID = "app_config") | ||
object AppConfig { | ||
var isFirstLaunch: Boolean by serial() | ||
} | ||
``` | ||
|
||
!!! failure "同名覆盖" | ||
当`mmapID`和字段名相同情况下会覆盖值, 甚至引起读取类型错误导致崩溃 | ||
|
||
## 全局配置 | ||
|
||
```kotlin | ||
Serialize.mmkv = MMKV.mmkvWithID("app_config") | ||
``` | ||
|
||
|
||
## 多账户存储 | ||
|
||
使用`SerializeHook`动态指定`mmapID`, 常见场景为根据账号使用不同用户数据 | ||
|
||
创建用户数据类 | ||
```kotlin | ||
@SerializeConfig(mmapID = "user_config") // 指定mmapID可以避免字段名重复情况下导致的错误 | ||
object UserConfig { | ||
var userId: String by serialLazy() | ||
} | ||
``` | ||
|
||
实现`SerializeHook` | ||
```kotlin | ||
class ProtobufSerializeHook : SerializeHook { | ||
|
||
// ... | ||
|
||
override fun mmkvWithID(mmapID: String, mode: Int, cryptKey: String?): MMKV { | ||
// 当存储为用户数据时, 添加当前账户到mmapID中, 请注意不要循环调用 | ||
if (mmapID == "user_config") { | ||
return super.mmkvWithID(mmapID + "_" + AppConfig.currentAccount, mode, cryptKey) | ||
} | ||
return super.mmkvWithID(mmapID, mode, cryptKey) | ||
} | ||
} | ||
``` | ||
|
||
## 指定存储目录/日志等级 | ||
|
||
默认情况下,MMKV 将文件存储在`$(FilesDir)/mmkv/` | ||
|
||
使用MMKV方法可指定自定义存储目录或日志输出等级 | ||
|
||
```kotlin | ||
MMKV.initialize(cacheDir.absolutePath, MMKVLogLevel.LevelInfo) // 存储路径, 日志等级 | ||
``` | ||
|
||
## 清除数据 | ||
|
||
两种方式清除数据 | ||
|
||
1. 赋值为null | ||
```kotlin | ||
var userId :String by serial() | ||
userId = null | ||
``` | ||
|
||
|
||
2. 指定`MMKV实例`删除 | ||
```kotlin | ||
MMKV.defaultMMKV().remove("字段名") | ||
MMKV.defaultMMKV().clearAll() | ||
MMKV.mmkvWithID("app_config").clearAll() | ||
``` |
Oops, something went wrong.