So, as we can see in the attached picture from the Google website
SingletonComponent will provide the same instance across the application.
Is this the same for ActivityComponent? Will it provide the same instance for all the fragments in that activity? I tried this code below, and I got two different ids of the navigator instance
// AppNavigator.kt
interface AppNavigator {
fun navigate()
}
// AppNavigatorImpl.kt
class AppNavigatorImpl @Inject constructor() : AppNavigator {
val id = UUID.randomUUID().toString()
override fun navigate() {
Log.d("AppNavigator", "Navigating with instance $id")
//navigation logic
}
}
// NavigationModule.kt
@InstallIn(ActivityComponent::class)
@Module
abstract class NavigationModule {
@Binds
abstract fun bindNavigator(impl: AppNavigatorImpl): AppNavigator
}
// MainActivity.kt
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var navigator: AppNavigator
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(navigator as AppNavigatorImpl).navigate()
}
}
// Fragment1.kt
@AndroidEntryPoint
class Fragment1 : Fragment() {
@Inject lateinit var navigator: AppNavigator
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(navigator as AppNavigatorImpl).navigate()
}
}