Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an IllegalStateException crash #1171

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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 {
Expand Down