SavedStateHandleController.attachToLifecycle : 已经附加到 lifecycleOwner

SavedStateHandleController.attachToLifecycle : Already attached to lifecycleOwner

我在具有非空构造函数的视图模型中使用 AbstractSavedStateViewModelFactory to use SavedStateHandle。但是我得到一个非法状态异常,说它已经附加到生命周期所有者(见下文)。当我改用 SavedStateViewModelFactory 时,它工作正常但它不支持非空构造函数并且是最终的 class.

TestStateAFragment

class TestStateAFragment : Fragment() {

    companion object {
        fun newInstance() : TestStateAFragment {
            val fragment = TestStateAFragment()
            val bundle = Bundle()
            bundle.putParcelable("arg_a", TestState.TestStateA)
            fragment.arguments = bundle
            return fragment
        }
    }

    private lateinit var viewModel : TestStateAViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val factory = TestStateAViewModelFactory(activity as SavedStateRegistryOwner)
        viewModel = ViewModelProviders.of(this, factory).get(TestStateAViewModel::class.java)
    }
}

TestStateAViewModel

class TestStateAViewModel(val savedStateHandle: SavedStateHandle) : ViewModel() {

    var countStateObserver : BehaviorRelay<Int> = BehaviorRelay.create<Int>()

    private var count : Int = 0
    set(value) {
        field = value
        savedStateHandle.set("count", field)
        countStateObserver.accept(field)
    }
    init {
        savedStateHandle.get<Int>("count")?.let {
            count = it
        }
    }

    fun incrementCount() {
        count += 1
    }

    fun decrementCount() {
        count -= 1
    }
}

TestStateAViewModelFactory

class TestStateAViewModelFactory(
    savedStateRegistryOwner: SavedStateRegistryOwner,
    bundle : Bundle? = null
) : AbstractSavedStateViewModelFactory(savedStateRegistryOwner, bundle) {

    override fun <T : ViewModel?> create(
        key: String,
        modelClass: Class<T>,
        handle: SavedStateHandle
    ): T {
        return TestStateAViewModel(handle) as T
    }
}

异常:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.lifecycleviewmodel, PID: 14295
    java.lang.IllegalStateException: Already attached to lifecycleOwner
        at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:43)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:177)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:145)
        at com.example.lifecycleviewmodel.fragment.states.a.TestStateAFragment.onCreate(TestStateAFragment.kt:47)
        at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
        at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
        at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
        at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at com.example.lifecycleviewmodel.TestActivity.onStart(TestActivity.kt:34)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1470)
        at android.app.Activity.performStart(Activity.java:7170)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3071)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

谁能指出问题出在哪里?谢谢。

编辑:以下是针对此特定错误提出的问题的链接。这已标记为已修复,预计将于本周 09/30-10/05 发布。 Issue-1 Issue-2

此问题已在 ViewModel 保存状态版本 1.0.0-beta01 中修复。 查找问题 here.

我知道这已经晚了,但可能会对其他人有所帮助。 使用
private val viewModel by activityViewModels()