From c2d681b11e58d7fca8ffc172366e395c5dfad7eb Mon Sep 17 00:00:00 2001 From: andrekir Date: Tue, 13 Sep 2022 01:25:36 -0300 Subject: [PATCH] fix null currentActivity --- .../com/geeksville/mesh/AppIntroduction.kt | 27 +++++++------------ .../java/com/geeksville/mesh/MainActivity.kt | 8 ++---- .../mesh/android/GeeksvilleApplication.kt | 26 +++++------------- 3 files changed, 19 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/AppIntroduction.kt b/app/src/main/java/com/geeksville/mesh/AppIntroduction.kt index f0512bbd..3608c568 100644 --- a/app/src/main/java/com/geeksville/mesh/AppIntroduction.kt +++ b/app/src/main/java/com/geeksville/mesh/AppIntroduction.kt @@ -1,10 +1,9 @@ package com.geeksville.mesh -import android.content.Context -import android.content.Intent -import android.content.SharedPreferences import android.os.Bundle +import androidx.core.content.edit import androidx.fragment.app.Fragment +import com.geeksville.mesh.model.UIViewModel import com.github.appintro.AppIntro import com.github.appintro.AppIntroFragment @@ -42,27 +41,21 @@ class AppIntroduction : AppIntro() { //addSlide(SlideTwoFragment()) } + private fun done() { + val prefs = UIViewModel.getPreferences(this) + prefs.edit { putBoolean("app_intro_completed", true) } + finish() + } + override fun onSkipPressed(currentFragment: Fragment?) { super.onSkipPressed(currentFragment) // Decide what to do when the user clicks on "Skip" - finish() - val preferences = getSharedPreferences("PREFERENCES", Context.MODE_PRIVATE) - var editor = preferences.edit() - editor.putBoolean("app_intro_completed", true) - editor.apply() - - startActivity(Intent(this, MainActivity::class.java)) + done() } override fun onDonePressed(currentFragment: Fragment?) { super.onDonePressed(currentFragment) // Decide what to do when the user clicks on "Done" - finish() - val preferences = getSharedPreferences("PREFERENCES", Context.MODE_PRIVATE) - var editor = preferences.edit() - editor.putBoolean("app_intro_completed", true) - editor.apply() - - startActivity(Intent(this, MainActivity::class.java)) + done() } } \ No newline at end of file diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 59863fd7..0e1fe1fc 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -263,20 +263,16 @@ class MainActivity : BaseActivity(), Logging { } override fun onCreate(savedInstanceState: Bundle?) { - val preferences = getSharedPreferences("PREFERENCES", Context.MODE_PRIVATE) - installSplashScreen() super.onCreate(savedInstanceState) - if (!preferences.getBoolean("app_intro_completed", false)) { + val prefs = UIViewModel.getPreferences(this) + if (!prefs.getBoolean("app_intro_completed", false)) { startActivity(Intent(this, AppIntroduction::class.java)) } binding = ActivityMainBinding.inflate(layoutInflater) - val prefs = UIViewModel.getPreferences(this) - model.setOwner(prefs.getString("owner", "")) - /// Set theme setUITheme(prefs) setContentView(binding.root) diff --git a/app/src/main/java/com/geeksville/mesh/android/GeeksvilleApplication.kt b/app/src/main/java/com/geeksville/mesh/android/GeeksvilleApplication.kt index d5aa4703..446a5585 100644 --- a/app/src/main/java/com/geeksville/mesh/android/GeeksvilleApplication.kt +++ b/app/src/main/java/com/geeksville/mesh/android/GeeksvilleApplication.kt @@ -32,10 +32,9 @@ open class GeeksvilleApplication( companion object { lateinit var analytics: AnalyticsProvider var currentActivity: Activity? = null + private val backstack = mutableListOf() } - var splunk: AnalyticsProvider? = null - private val lifecycleCallbacks = object : ActivityLifecycleCallbacks { override fun onActivityPaused(activity: Activity) { } @@ -44,7 +43,8 @@ open class GeeksvilleApplication( } override fun onActivityDestroyed(activity: Activity) { - currentActivity = null + if (backstack.contains(activity)) backstack.remove(activity) + currentActivity = backstack.lastOrNull() } override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { @@ -54,7 +54,8 @@ open class GeeksvilleApplication( } override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { - currentActivity = activity + backstack.add(activity) + currentActivity = backstack.lastOrNull() } override fun onActivityResumed(activity: Activity) { @@ -72,16 +73,13 @@ open class GeeksvilleApplication( } private val analyticsPrefs: SharedPreferences by lazy { - getSharedPreferences( - "analytics-prefs", - Context.MODE_PRIVATE - ) + getSharedPreferences("analytics-prefs", Context.MODE_PRIVATE) } var isAnalyticsAllowed: Boolean get() = analyticsPrefs.getBoolean("allowed", true) set(value) { - analyticsPrefs.edit(commit = true) { + analyticsPrefs.edit { putBoolean("allowed", value) } @@ -112,13 +110,3 @@ open class GeeksvilleApplication( } } - - -fun geeksvilleApp(context: Context) = context.applicationContext as GeeksvilleApplication - - -interface GeeksvilleApplicationClient { - - fun getAnalytics() = GeeksvilleApplication.analytics - -} \ No newline at end of file