diff --git a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt index 11827b03c..ab5c28138 100644 --- a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt +++ b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt @@ -28,6 +28,7 @@ import org.koin.sample.sandbox.mvvm.MVVMFragment import org.koin.sample.sandbox.navigation.NavViewModel import org.koin.sample.sandbox.navigation.NavViewModel2 import org.koin.sample.sandbox.scope.ScopedActivityA +import org.koin.sample.sandbox.scope.ScopedFragment import org.koin.sample.sandbox.workmanager.SimpleWorker import org.koin.sample.sandbox.workmanager.SimpleWorkerService @@ -106,9 +107,13 @@ val scopeModule = lazyModule { val scopeModuleActivityA = lazyModule { scope { + fragmentOf(::ScopedFragment) scopedOf(::Session) scopedOf(::SessionActivity) } + scope { + + } } val workerServiceModule = lazyModule { diff --git a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedActivityA.kt b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedActivityA.kt index 1ebaedffa..1e2be2285 100644 --- a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedActivityA.kt +++ b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedActivityA.kt @@ -5,11 +5,13 @@ import android.widget.Button import org.koin.android.ext.android.get import org.koin.android.ext.android.getKoin import org.koin.android.ext.android.inject +import org.koin.androidx.fragment.android.replace import org.koin.androidx.scope.RetainedScopeActivity import org.koin.core.qualifier.named import org.koin.sample.sandbox.R import org.koin.sample.sandbox.components.* import org.koin.sample.sandbox.components.scope.Session +import org.koin.sample.sandbox.mvvm.MVVMFragment import org.koin.sample.sandbox.utils.navigateTo class ScopedActivityA : RetainedScopeActivity(R.layout.scoped_activity_a) { @@ -20,6 +22,10 @@ class ScopedActivityA : RetainedScopeActivity(R.layout.scoped_activity_a) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + supportFragmentManager.beginTransaction() + .replace(R.id.mvvm_frame) + .commit() + assert(currentSession == get()) if (SESSION_ID_VAR.isEmpty()) { println("Create ID for session: $SESSION_ID_VAR") diff --git a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedFragment.kt b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedFragment.kt new file mode 100644 index 000000000..19edc634f --- /dev/null +++ b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/scope/ScopedFragment.kt @@ -0,0 +1,28 @@ +package org.koin.sample.sandbox.scope + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.Fragment +import org.koin.android.scope.AndroidScopeComponent +import org.koin.androidx.scope.fragmentScope +import org.koin.androidx.scope.getRetainedScopeOrNull +import org.koin.core.scope.Scope +import org.koin.sample.sandbox.R +import org.koin.sample.sandbox.components.scope.Session + +class ScopedFragment : Fragment(R.layout.mvvm_fragment), AndroidScopeComponent { + + override val scope: Scope by fragmentScope() + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + checks() + } + + private fun checks() { + assert( + scope.get() == requireActivity().getRetainedScopeOrNull()?.get() + ) + } +} \ No newline at end of file diff --git a/examples/androidx-samples/src/main/res/layout/scoped_activity_a.xml b/examples/androidx-samples/src/main/res/layout/scoped_activity_a.xml index e10a5d0b3..57e5baa0d 100644 --- a/examples/androidx-samples/src/main/res/layout/scoped_activity_a.xml +++ b/examples/androidx-samples/src/main/res/layout/scoped_activity_a.xml @@ -18,4 +18,9 @@ android:layout_height="wrap_content" android:text="Next" /> + + \ No newline at end of file