Android Jetpack

Posted on September 2, 2022
Tags: codeetc

1 Summary

Android uses UI - ViewModel - Repo stack which is MVVM (Model View ViewModel)

2 Activity

3 Intent and Intent Filters

4 Context

5 BackStack

Launch Modes

6 ViewModel

7 DI

Modern DI libraries goes a step further and does automatic instantiation of the parameter, meaning y is set a value automatically.

8 install library

go to libs.version.toml

[versions]

[libraries]

[plugins]

9 Hilt

@HiltAndroidApp
class MyApp: Application()
<application>
    android:name=".MyApp"
    ...

9.1 Dependency Injection Module (Producers)

  • @Module tell us the object the contains dependencies(Producers) for injection.
    • @InstallIn() tells us when the dependencies are created/destroyed
    • @InstallIn(SingletonComponent::class) tell us the dependencies lives for the whole app
      • Not A Real Singleton: This DOES NOT make the dependencies itself a singleton
    • @InstallIn(ActivityComponent::class) tell us the dependencies live only for the activity
    • @InstallIn(ViewModelComponent::class) tell us the dependencies live only for the ViewModel

provideMyApi(): MyApi is a Producer dependency function declared under the Hilt @Module Producer meaning whenever a Consumer class needs a MyApi type it can be injected(Auto-instantiated) with @Inject

  • Producer dependency function provideMyApi(): MyApi annotations
    • @Provides tells us this is a dependency
    • @Singleton is an actual singleton
      • without it: if you have multiple Consumer that wants to be injected, each Consumer would get a new instantiation of MyApi.

Note: You will NEVER call the producer functions declared in @Module such as provideMyApi or provideMyRepository

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    
    @Provides
    @Singleton
    fun provideMyApi(): MyApi {
        return Bleh
    }

    @Provides
    @Singleton
    fun provideMyRepository(api: MyApi): MyRepository {
        //notice it passes another Dependency Producer MyApi as argument
        //Since they are both in @Module MyApi will get injected from provideMyApi()
        return api
    }

    @Provides
    @Singleton
    fun provideSomethingElse(app: Application) : SomethingElse {
        //remember @HiltAndroidApp annotation lets us use android application context in dependencies
        return Bleh(app)
    }

}

9.2 Class @Inject (Consumers)

  • @HiltViewModel tells us this class is a Consumer that wants dependencies to get injected in
@HiltViewModel
class MyViewModel @Inject constructor(
    private val repository: MyRepository
): ViewModel() {

}

9.3 DI with Hilt

import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

class MyText @Inject constructor() {
    val text = "Hello from Hilt!"
}
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject
    lateinit var myText: MyText
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        enableEdgeToEdge()
        setContent {
            DemoOneTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Column{

                        Greeting(
                            name = "hiilo",
                            modifier = Modifier.padding(innerPadding)
                        )
                        Log.d("MainActivity", "Injected text: ${myText.text}")
                        Text(myText.text)

                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    DemoOneTheme {
        Greeting("Android")
    }
}