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
import toothpick.configuration.Configuration
import toothpick.registries.FactoryRegistryLocator
import toothpick.registries.MemberInjectorRegistryLocator
import java.util.*
/**
* @author Konstantin Tskhovrebov (aka terrakok) on 26.03.17.
......@@ -22,6 +23,7 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
appCode = UUID.randomUUID().toString()
initLogger()
initFabric()
......@@ -75,4 +77,9 @@ class App : Application() {
private fun initThreetenABP() {
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
import android.view.LayoutInflater
import android.view.ViewGroup
import com.arellomobile.mvp.MvpAppCompatFragment
import ru.terrakok.gitlabclient.App
import ru.terrakok.gitlabclient.extension.objectScopeName
import timber.log.Timber
import toothpick.Scope
......@@ -12,6 +13,7 @@ import toothpick.Toothpick
private const val PROGRESS_TAG = "bf_progress"
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"
/**
......@@ -32,13 +34,16 @@ abstract class BaseFragment : MvpAppCompatFragment() {
protected open val scopeModuleInstaller: (Scope) -> Unit = {}
private lateinit var fragmentScopeName: String
private var scopeIsNotInit: Boolean = true
protected lateinit var scope: Scope
private set
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()
scope = Toothpick.openScopes(parentScopeName, fragmentScopeName)
.apply {
......@@ -76,6 +81,7 @@ abstract class BaseFragment : MvpAppCompatFragment() {
super.onSaveInstanceState(outState)
instanceStateSaved = true
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
}
......
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