fix: Scope getting removed when switching between settings 'Kelvin approved'

This commit is contained in:
zvonimir 2025-06-06 15:40:43 +02:00
commit 0d9e1cd3c5
4 changed files with 23 additions and 12 deletions

View file

@ -115,6 +115,7 @@ import java.io.StringWriter
import java.lang.reflect.InvocationTargetException import java.lang.reflect.InvocationTargetException
import java.util.LinkedList import java.util.LinkedList
import java.util.Queue import java.util.Queue
import java.util.UUID
import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.ConcurrentLinkedQueue
@ -218,6 +219,8 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
} }
} }
val mainId = UUID.randomUUID().toString().substring(0, 5)
constructor() : super() { constructor() : super() {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
StrictMode.setVmPolicy( StrictMode.setVmPolicy(
@ -269,8 +272,8 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
@UnstableApi @UnstableApi
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Logger.i(TAG, "MainActivity Starting"); Logger.w(TAG, "MainActivity Starting [$mainId]");
StateApp.instance.setGlobalContext(this, lifecycleScope); StateApp.instance.setGlobalContext(this, lifecycleScope, mainId);
StateApp.instance.mainAppStarting(this); StateApp.instance.mainAppStarting(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -671,13 +674,13 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
override fun onResume() { override fun onResume() {
super.onResume(); super.onResume();
Logger.v(TAG, "onResume") Logger.w(TAG, "onResume [$mainId]")
_isVisible = true; _isVisible = true;
} }
override fun onPause() { override fun onPause() {
super.onPause(); super.onPause();
Logger.v(TAG, "onPause") Logger.w(TAG, "onPause [$mainId]")
_isVisible = false; _isVisible = false;
_qrCodeLoadingDialog?.dismiss() _qrCodeLoadingDialog?.dismiss()
@ -686,7 +689,7 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
Logger.v(TAG, "_wasStopped = true"); Logger.w(TAG, "onStop [$mainId]");
_wasStopped = true; _wasStopped = true;
} }
@ -1103,8 +1106,8 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy(); super.onDestroy();
Logger.v(TAG, "onDestroy") Logger.w(TAG, "onDestroy [$mainId]")
StateApp.instance.mainAppDestroyed(this); StateApp.instance.mainAppDestroyed(this, mainId);
} }
inline fun <reified T> isFragmentActive(): Boolean { inline fun <reified T> isFragmentActive(): Boolean {

View file

@ -156,6 +156,8 @@ class StateApp {
return thisContext; return thisContext;
} }
private var _mainId: String? = null;
//Files //Files
private var _tempDirectory: File? = null; private var _tempDirectory: File? = null;
private var _cacheDirectory: File? = null; private var _cacheDirectory: File? = null;
@ -295,9 +297,12 @@ class StateApp {
} }
//Lifecycle //Lifecycle
fun setGlobalContext(context: Context, coroutineScope: CoroutineScope? = null) { fun setGlobalContext(context: Context, coroutineScope: CoroutineScope? = null, mainId: String? = null) {
_mainId = mainId;
_context = context; _context = context;
_scope = coroutineScope _scope = coroutineScope
Logger.w(TAG, "Scope initialized ${(coroutineScope != null)}\n ${Log.getStackTraceString(Throwable())}")
} }
fun initializeFiles(force: Boolean = false) { fun initializeFiles(force: Boolean = false) {
@ -719,7 +724,9 @@ class StateApp {
migrateStores(context, managedStores, index + 1); migrateStores(context, managedStores, index + 1);
} }
fun mainAppDestroyed(context: Context) { fun mainAppDestroyed(context: Context, mainId: String? = null) {
if (mainId != null && (_mainId != mainId || _mainId == null))
return
Logger.i(TAG, "App ended"); Logger.i(TAG, "App ended");
_receiverBecomingNoisy?.let { _receiverBecomingNoisy?.let {
_receiverBecomingNoisy = null; _receiverBecomingNoisy = null;
@ -743,7 +750,8 @@ class StateApp {
fun dispose(){ fun dispose(){
_context = null; _context = null;
_scope = null; // _scope = null;
Logger.w(TAG, "StateApp disposed: ${Log.getStackTraceString(Throwable())}")
} }
private val _connectivityEvents = object : ConnectivityManager.NetworkCallback() { private val _connectivityEvents = object : ConnectivityManager.NetworkCallback() {

View file

@ -90,7 +90,7 @@ class ToggleField : TableRow, IField {
val advancedFieldAttr = field.getAnnotation(AdvancedField::class.java) val advancedFieldAttr = field.getAnnotation(AdvancedField::class.java)
if(advancedFieldAttr != null || advanced) { if(advancedFieldAttr != null || advanced) {
Logger.w("ToggleField", "Found advanced field: " + field.name); // Logger.w("ToggleField", "Found advanced field: " + field.name);
isAdvanced = true; isAdvanced = true;
} }

View file

@ -7,7 +7,7 @@
<application> <application>
<receiver android:name=".receivers.InstallReceiver" /> <receiver android:name=".receivers.InstallReceiver" />
<activity android:name=".activities.MainActivity"> <activity android:name=".activities.MainActivity" android:launchMode="singleInstance">
<intent-filter android:autoVerify="true"> <intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
<action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" /> <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" />