diff --git a/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt b/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt index 2ad51618..1ea8f23a 100644 --- a/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt +++ b/formula-android/src/main/java/com/instacart/formula/android/FormulaFragment.kt @@ -1,5 +1,6 @@ package com.instacart.formula.android +import android.content.Context import android.os.Bundle import android.os.SystemClock import android.view.LayoutInflater @@ -30,7 +31,7 @@ class FormulaFragment : Fragment(), BaseFormulaFragment { requireArguments().getParcelable(ARG_CONTRACT)!! } - private val initializedAtMillis = SystemClock.uptimeMillis() + private var initializedAtMillis: Long? = SystemClock.uptimeMillis() private var featureView: FeatureView? = null private val stateRelay: BehaviorRelay = BehaviorRelay.create() @@ -38,6 +39,13 @@ class FormulaFragment : Fragment(), BaseFormulaFragment { private var lifecycleCallback: FragmentLifecycleCallback? = null + override fun onAttach(context: Context) { + super.onAttach(context) + if (initializedAtMillis == null) { + initializedAtMillis = SystemClock.uptimeMillis() + } + } + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val viewFactory = FormulaFragmentDelegate.viewFactory(this) ?: run { // No view factory, no view @@ -53,7 +61,7 @@ class FormulaFragment : Fragment(), BaseFormulaFragment { super.onViewCreated(view, savedInstanceState) featureView?.let { value -> val state = FeatureView.State( - initializedAtMillis = initializedAtMillis, + initializedAtMillis = initializedAtMillis ?: SystemClock.uptimeMillis(), fragmentId = getFormulaFragmentId(), environment = FormulaFragmentDelegate.fragmentEnvironment(), observable = stateRelay, @@ -100,6 +108,8 @@ class FormulaFragment : Fragment(), BaseFormulaFragment { } override fun onDestroyView() { + initializedAtMillis = null + cancelable?.cancel() cancelable = null