mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Fix an IllegalStateException crash
When you play something, then go home on the device, and come back to something that should run popCurrentPage(), IE `noLinksFound()`, it often causes the app to crash into safe mode with IllegalStateException. This may not be the best solution here, but testing seems to indicate it does work to fix this. I thought about using `supportFragmentManager.popBackStack()`, but that seems to cause a bigger delay in doing anything, even if we don't have any issues here. Also `onBackPressedDispatcher.onBackPressed()` can be more in line with how Android handles back press actions, respecting the back stack and fragment lifecycle more conveniently so went with this approach.
This commit is contained in:
parent
5f64e40a7e
commit
18a535ed3e
1 changed files with 19 additions and 1 deletions
|
@ -16,6 +16,8 @@ import android.graphics.Color
|
|||
import android.graphics.drawable.Drawable
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.TransactionTooLargeException
|
||||
import android.util.Log
|
||||
import android.view.*
|
||||
|
@ -475,7 +477,23 @@ object UIHelper {
|
|||
}
|
||||
|
||||
fun FragmentActivity.popCurrentPage() {
|
||||
this.onBackPressedDispatcher.onBackPressed()
|
||||
// Post the back press action to the main thread handler to ensure it executes
|
||||
// after any currently pending UI updates or fragment transactions.
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
// Check if the FragmentManager state is saved. If it is, we cannot perform
|
||||
// fragment transactions safely because the state may be inconsistent.
|
||||
if (!supportFragmentManager.isStateSaved) {
|
||||
// If the state is not saved, it's safe to perform the back press action.
|
||||
this.onBackPressedDispatcher.onBackPressed()
|
||||
} else {
|
||||
// If the state is saved, retry the back press action after a slight delay.
|
||||
// This gives the FragmentManager time to complete any ongoing state-saving
|
||||
// operations or transactions, ensuring that we do not encounter an IllegalStateException.
|
||||
Handler(Looper.getMainLooper()).postDelayed({
|
||||
this.onBackPressedDispatcher.onBackPressed()
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getStatusBarHeight(): Int {
|
||||
|
|
Loading…
Reference in a new issue