-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleFixed.kt
61 lines (51 loc) · 1.85 KB
/
SimpleFixed.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.collection.LruCache
import java.util.UUID
/**
* kotlin version
* [Thanks](https://juejin.cn/post/7306388118914973734)
*/
object SimpleFixed {
private const val KEY = "TransactionTooLargeOptKey"
private var map: LruCache<String, Bundle> = LruCache(32)
@JvmStatic
fun init(application: Application) {
application.registerActivityLifecycleCallbacks(object :
DefaultActivityLifecycleCallbacks() {
override fun onActivityPostSaveInstanceState(activity: Activity, outState: Bundle) {
val uuid = UUID.randomUUID().toString()
val newBundle = Bundle()
newBundle.putAll(outState)
map.put(uuid, newBundle)
outState.clear()
outState.putString(KEY, uuid)
}
override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) {
savedInstanceState?.let {
val uuid = it.getString(KEY) ?: return
val bundle = map.get(uuid)
savedInstanceState.clear()
savedInstanceState.putAll(bundle)
}
}
})
}
}
open class DefaultActivityLifecycleCallbacks : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
}
override fun onActivityStarted(activity: Activity) {
}
override fun onActivityResumed(activity: Activity) {
}
override fun onActivityPaused(activity: Activity) {
}
override fun onActivityStopped(activity: Activity) {
}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
}
override fun onActivityDestroyed(activity: Activity) {
}
}