From 35572a0906ed66ed714c5ff9895cc794114a41a7 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 4 May 2020 13:21:04 +0200 Subject: [PATCH] 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) --- .../src/main/AndroidManifest.xml | 23 +++++++++-------- .../banking/fints4java/android/BankingApp.kt | 25 +++++++++++++++++++ .../fints4java/android/MainActivity.kt | 11 +------- .../fints4java/android/di/BankingModule.kt | 7 +++--- 4 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/BankingApp.kt diff --git a/fints4javaAndroidApp/src/main/AndroidManifest.xml b/fints4javaAndroidApp/src/main/AndroidManifest.xml index bfeb6746..6a7d0f0c 100644 --- a/fints4javaAndroidApp/src/main/AndroidManifest.xml +++ b/fints4javaAndroidApp/src/main/AndroidManifest.xml @@ -6,17 +6,20 @@ + android:name=".BankingApp" + android:testOnly="false" + android:allowBackup="true" + android:icon="@drawable/app_icon" + android:label="@string/app_name" + android:roundIcon="@mipmap/ic_launcher_round" + android:supportsRtl="true" + android:theme="@style/AppTheme"> + + android:name=".MainActivity" + android:label="@string/app_name" + android:theme="@style/AppTheme.NoActionBar"> + diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/BankingApp.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/BankingApp.kt new file mode 100644 index 00000000..1322e209 --- /dev/null +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/BankingApp.kt @@ -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 + } + +} \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt index ad722449..1fdbb052 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt @@ -11,7 +11,6 @@ import com.github.clans.fab.FloatingActionMenu import kotlinx.android.synthetic.main.activity_main.* import net.dankito.banking.fints4java.android.di.BankingComponent 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.MainActivityFloatingActionMenuButton import net.dankito.banking.ui.presenter.BankingPresenter @@ -42,21 +41,13 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setupDependencyInjection() + BankingModule.mainActivity = this BankingComponent.component.inject(this) initUi() } - private fun setupDependencyInjection() { - val component = DaggerBankingComponent.builder() - .bankingModule(BankingModule(this)) - .build() - - BankingComponent.component = component - } - private fun initUi() { setContentView(R.layout.activity_main) diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt index 4ade2bad..c148d786 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt @@ -30,7 +30,7 @@ import javax.inject.Singleton @Module -class BankingModule(internal val mainActivity: AppCompatActivity) { +class BankingModule(private val applicationContext: Context) { companion object { @@ -40,12 +40,11 @@ class BankingModule(internal val mainActivity: AppCompatActivity) { const val IndexFolderKey = "index.folder" + // TODO: implement activity listener to always get latest activity + lateinit var mainActivity: AppCompatActivity } - private val applicationContext = mainActivity.applicationContext - - @Provides @Singleton fun provideApplicationContext() : Context {