Implemented setting up dependency injection in Application class to fix at least in first instance that when restoring a Dialog that MainActivity hasn't been restored and therefore dependency injection not instantiated yet (still missing: set latest Activity in BankingModule / RouterAndroid)

This commit is contained in:
dankito 2020-05-04 13:21:04 +02:00
parent 07144c2a2d
commit 35572a0906
4 changed files with 42 additions and 24 deletions

View File

@ -6,17 +6,20 @@
<application <application
android:testOnly="false" android:name=".BankingApp"
android:allowBackup="true" android:testOnly="false"
android:icon="@drawable/app_icon" android:allowBackup="true"
android:label="@string/app_name" android:icon="@drawable/app_icon"
android:roundIcon="@mipmap/ic_launcher_round" android:label="@string/app_name"
android:supportsRtl="true" android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme"> android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@ -0,0 +1,25 @@
package net.dankito.banking.fints4java.android
import android.app.Application
import net.dankito.banking.fints4java.android.di.BankingComponent
import net.dankito.banking.fints4java.android.di.BankingModule
import net.dankito.banking.fints4java.android.di.DaggerBankingComponent
class BankingApp : Application() {
override fun onCreate() {
super.onCreate()
setupDependencyInjection()
}
private fun setupDependencyInjection() {
val component = DaggerBankingComponent.builder()
.bankingModule(BankingModule(this))
.build()
BankingComponent.component = component
}
}

View File

@ -11,7 +11,6 @@ import com.github.clans.fab.FloatingActionMenu
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import net.dankito.banking.fints4java.android.di.BankingComponent import net.dankito.banking.fints4java.android.di.BankingComponent
import net.dankito.banking.fints4java.android.di.BankingModule import net.dankito.banking.fints4java.android.di.BankingModule
import net.dankito.banking.fints4java.android.di.DaggerBankingComponent
import net.dankito.banking.fints4java.android.ui.views.DrawerView import net.dankito.banking.fints4java.android.ui.views.DrawerView
import net.dankito.banking.fints4java.android.ui.views.MainActivityFloatingActionMenuButton import net.dankito.banking.fints4java.android.ui.views.MainActivityFloatingActionMenuButton
import net.dankito.banking.ui.presenter.BankingPresenter import net.dankito.banking.ui.presenter.BankingPresenter
@ -42,21 +41,13 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setupDependencyInjection() BankingModule.mainActivity = this
BankingComponent.component.inject(this) BankingComponent.component.inject(this)
initUi() initUi()
} }
private fun setupDependencyInjection() {
val component = DaggerBankingComponent.builder()
.bankingModule(BankingModule(this))
.build()
BankingComponent.component = component
}
private fun initUi() { private fun initUi() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)

View File

@ -30,7 +30,7 @@ import javax.inject.Singleton
@Module @Module
class BankingModule(internal val mainActivity: AppCompatActivity) { class BankingModule(private val applicationContext: Context) {
companion object { companion object {
@ -40,12 +40,11 @@ class BankingModule(internal val mainActivity: AppCompatActivity) {
const val IndexFolderKey = "index.folder" const val IndexFolderKey = "index.folder"
// TODO: implement activity listener to always get latest activity
lateinit var mainActivity: AppCompatActivity
} }
private val applicationContext = mainActivity.applicationContext
@Provides @Provides
@Singleton @Singleton
fun provideApplicationContext() : Context { fun provideApplicationContext() : Context {