Commit 271c381b authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Fix fragment scope life cycle.

Reinitialize scope after process death.
parent 3815b36f
...@@ -14,6 +14,7 @@ import toothpick.Toothpick ...@@ -14,6 +14,7 @@ import toothpick.Toothpick
import toothpick.configuration.Configuration import toothpick.configuration.Configuration
import toothpick.registries.FactoryRegistryLocator import toothpick.registries.FactoryRegistryLocator
import toothpick.registries.MemberInjectorRegistryLocator import toothpick.registries.MemberInjectorRegistryLocator
import java.util.*
/** /**
* @author Konstantin Tskhovrebov (aka terrakok) on 26.03.17. * @author Konstantin Tskhovrebov (aka terrakok) on 26.03.17.
...@@ -22,6 +23,7 @@ class App : Application() { ...@@ -22,6 +23,7 @@ class App : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
appCode = UUID.randomUUID().toString()
initLogger() initLogger()
initFabric() initFabric()
...@@ -75,4 +77,9 @@ class App : Application() { ...@@ -75,4 +77,9 @@ class App : Application() {
private fun initThreetenABP() { private fun initThreetenABP() {
AndroidThreeTen.init(this) AndroidThreeTen.init(this)
} }
companion object {
lateinit var appCode: String
private set
}
} }
\ No newline at end of file
...@@ -5,6 +5,7 @@ import android.os.Handler ...@@ -5,6 +5,7 @@ import android.os.Handler
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import com.arellomobile.mvp.MvpAppCompatFragment import com.arellomobile.mvp.MvpAppCompatFragment
import ru.terrakok.gitlabclient.App
import ru.terrakok.gitlabclient.extension.objectScopeName import ru.terrakok.gitlabclient.extension.objectScopeName
import timber.log.Timber import timber.log.Timber
import toothpick.Scope import toothpick.Scope
...@@ -12,6 +13,7 @@ import toothpick.Toothpick ...@@ -12,6 +13,7 @@ import toothpick.Toothpick
private const val PROGRESS_TAG = "bf_progress" private const val PROGRESS_TAG = "bf_progress"
private const val STATE_SCOPE_NAME = "state_scope_name" private const val STATE_SCOPE_NAME = "state_scope_name"
private const val STATE_LAUNCH_FLAG = "state_launch_flag"
private const val STATE_SCOPE_WAS_CLOSED = "state_scope_was_closed" private const val STATE_SCOPE_WAS_CLOSED = "state_scope_was_closed"
/** /**
...@@ -32,13 +34,16 @@ abstract class BaseFragment : MvpAppCompatFragment() { ...@@ -32,13 +34,16 @@ abstract class BaseFragment : MvpAppCompatFragment() {
protected open val scopeModuleInstaller: (Scope) -> Unit = {} protected open val scopeModuleInstaller: (Scope) -> Unit = {}
private lateinit var fragmentScopeName: String private lateinit var fragmentScopeName: String
private var scopeIsNotInit: Boolean = true
protected lateinit var scope: Scope protected lateinit var scope: Scope
private set private set
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
val savedAppCode = savedInstanceState?.getString(STATE_LAUNCH_FLAG)
//False - if fragment was restored without new app process (for example: activity rotation)
val isNewInAppProcess = savedAppCode != App.appCode
val scopeWasClosed = savedInstanceState?.getBoolean(STATE_SCOPE_WAS_CLOSED) ?: true
scopeIsNotInit = savedInstanceState?.getBoolean(STATE_SCOPE_WAS_CLOSED) ?: true val scopeIsNotInit = isNewInAppProcess || scopeWasClosed
fragmentScopeName = savedInstanceState?.getString(STATE_SCOPE_NAME) ?: objectScopeName() fragmentScopeName = savedInstanceState?.getString(STATE_SCOPE_NAME) ?: objectScopeName()
scope = Toothpick.openScopes(parentScopeName, fragmentScopeName) scope = Toothpick.openScopes(parentScopeName, fragmentScopeName)
.apply { .apply {
...@@ -76,6 +81,7 @@ abstract class BaseFragment : MvpAppCompatFragment() { ...@@ -76,6 +81,7 @@ abstract class BaseFragment : MvpAppCompatFragment() {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
instanceStateSaved = true instanceStateSaved = true
outState.putString(STATE_SCOPE_NAME, fragmentScopeName) outState.putString(STATE_SCOPE_NAME, fragmentScopeName)
outState.putString(STATE_LAUNCH_FLAG, App.appCode)
outState.putBoolean(STATE_SCOPE_WAS_CLOSED, needCloseScope()) //save it but will be used only if destroyed outState.putBoolean(STATE_SCOPE_WAS_CLOSED, needCloseScope()) //save it but will be used only if destroyed
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment