Detailed explanation and use of SavedStateHandle data storage of Android MVVM.

1. Introduction

        As can be seen from the name, SavedStateHandle saves the state. This class is often used in conjunction with ViewModel in MVVM to make a container for caching and restoring the data state of the page life cycle. This is easier than onSaveInstanceState(Bundle), the saved data types are also richer, and the amount of data is relatively large

2. Analysis

        The easiest way to analyze a class is to start from the source code:

2.1 Container collection

  1. regular = mutableMapOf
  2. savedStateProviders = mutableMapOf
  3. liveDatas = mutableMapOf
  4. flows = mutableMapOf

The above are all map collections, which are used to store key-values. The four collections correspond to four different scenarios.

2.2 Introduction to AIP

fun <K> set(key: String, value: K, bundle: Bundle) {
    state.set(key, value)

    state.setSavedStateProvider(key, MyProvider(bundle))
    state.set(key, MyStateFlow<K>())

}

    fun <M> get(key: String) {
        state.get<M>(key)
        state.getLiveData<M>(key)
        var initialValue: M? = null
        state.getLiveData(key, initialValue)
        state.getStateFlow(key, initialValue)
    }

        Through data saving, we will find that there is no LiveData type when setting, that is because the livedata collection will be processed separately in the set method

source code

 Third, the introduction of the class

3.1 SavedStateRegistry.SavedStateProvider

This class is an interface that provides a method that returns a bundle object. Therefore, the original bundle object is saved through this method

3.2 StateFlow

Flow is a flow, and stateflow is a state flow. In the software life cycle, bytes and streams have always been relatively common resources, especially streams, which can accompany any business and module.

SavedStateHandle also supports stream saving.

Regarding the use of out in SateFlow, I will explain out and in

out:

Denoted by out in kotlin, "out T" in kotlin is equivalent to Java's <? extends T>

in:

Denoted by in in kotlin, "in T" in kotlin is equivalent to Java's <? super T>

    internal class MyStateFlow<out A> : StateFlow<A> {

        //重复缓存的快照
        override val replayCache: List<A>
            get() = TODO("Not yet implemented")

        /**
         * 接受给定的收集器并将值发送到其中。要将值从共享流发送到特定收集器,可以使用collecter.emitAll(流)或collect{…}SAM转换。

        共享流永远不会完成。对Flow.collector或共享流上的任何其他终端操作员的调用从未正常完成。
         * */
        override suspend fun collect(collector: FlowCollector<A>): Nothing {
            TODO("Not yet implemented")
            var a:A
            collector.emit(a)
        }
        //此状态流的当前值
        override val value: A
            get() = TODO("Not yet implemented")
    }

Four. Summary

        There are two ways to save data, but there are four ways to store data. Therefore, whether it is saving or restoring, the rules of SavedStateHandle need to be strictly enforced.

Guess you like

Origin blog.csdn.net/qq36246172/article/details/128582869