Fix issue where DownloadedPlayerActivity interferes with MainActivity

This commit is contained in:
Luna712 2023-10-08 17:00:44 -06:00
parent 33eb3a3b29
commit 9933b5adbe
3 changed files with 42 additions and 5 deletions

View file

@ -64,6 +64,35 @@ object CommonActivity {
private set(value) {
_activity = WeakReference(value)
}
private val activeActivities = mutableListOf<WeakReference<Activity>>() // Keep track of active activities
@MainThread
fun registerActivity(activity: Activity) {
activeActivities.add(WeakReference(activity))
}
@MainThread
fun unregisterActivity(activity: Activity) {
val iterator = activeActivities.iterator()
while (iterator.hasNext()) {
val weakReference = iterator.next()
val storedActivity = weakReference.get()
if (storedActivity == null || storedActivity == activity) {
iterator.remove()
}
}
}
@MainThread
fun getCurrentActivity(): Activity? {
for (weakReference in activeActivities) {
val activity = weakReference.get()
if (activity != null && !activity.isFinishing) {
return activity
}
}
return null
}
@MainThread
fun Activity?.getCastSession(): CastSession? {
@ -203,8 +232,9 @@ object CommonActivity {
setLocale(this, localeCode)
}
fun init(act: ComponentActivity?) {
if (act == null) return
fun init() {
val act = getCurrentActivity() as? ComponentActivity ?: return
activity = act
//https://stackoverflow.com/questions/52594181/how-to-know-if-user-has-disabled-picture-in-picture-feature-permission
//https://developer.android.com/guide/topics/ui/picture-in-picture

View file

@ -681,6 +681,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
broadcastIntent.setClass(this, VideoDownloadRestartReceiver::class.java)
this.sendBroadcast(broadcastIntent)
afterPluginsLoadedEvent -= ::onAllPluginsLoaded
CommonActivity.unregisterActivity(this)
super.onDestroy()
}
@ -1369,8 +1370,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
// val navView: BottomNavigationView = findViewById(R.id.nav_view)
setUpBackup()
CommonActivity.init(this)
CommonActivity.registerActivity(this)
CommonActivity.init()
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

View file

@ -74,7 +74,8 @@ class DownloadedPlayerActivity : AppCompatActivity() {
CommonActivity.loadThemes(this)
super.onCreate(savedInstanceState)
CommonActivity.init(this)
CommonActivity.registerActivity(this)
CommonActivity.init()
setContentView(R.layout.empty_layout)
@ -110,4 +111,9 @@ class DownloadedPlayerActivity : AppCompatActivity() {
return
}
}
override fun onDestroy() {
CommonActivity.unregisterActivity(this)
super.onDestroy()
}
}