fragment health check
This commit is contained in:
+19
-6
@@ -181,34 +181,47 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
|
|||||||
|
|
||||||
val fragmentActivity = activity as? FragmentActivity ?: return false
|
val fragmentActivity = activity as? FragmentActivity ?: return false
|
||||||
|
|
||||||
// Check if activity is in valid state
|
|
||||||
if (fragmentActivity.isFinishing || fragmentActivity.isDestroyed) {
|
if (fragmentActivity.isFinishing || fragmentActivity.isDestroyed) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val container = fragmentActivity.findViewById<View>(FRAGMENT_CONTAINER_ID) ?: return false
|
fragmentActivity.findViewById<View>(FRAGMENT_CONTAINER_ID) ?: return false
|
||||||
|
|
||||||
val fm = fragmentActivity.supportFragmentManager
|
val fm = fragmentActivity.supportFragmentManager
|
||||||
|
|
||||||
// Ensure FragmentManager is in valid state
|
|
||||||
if (fm.isStateSaved) {
|
if (fm.isStateSaved) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val existingFragment = fm.findFragmentById(FRAGMENT_CONTAINER_ID)
|
val existingFragment = fm.findFragmentById(FRAGMENT_CONTAINER_ID)
|
||||||
if (existingFragment is BrowserFragment) {
|
if (existingFragment is BrowserFragment) {
|
||||||
// Fragment already replaced, no need to do it again
|
// Check if fragment needs engine refresh instead of full replacement
|
||||||
return true
|
if (!isFragmentCorrupted(existingFragment)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val nativeFragment = BrowserFragment.create()
|
val nativeFragment = BrowserFragment.create()
|
||||||
fm.beginTransaction()
|
fm.beginTransaction()
|
||||||
.replace(FRAGMENT_CONTAINER_ID, nativeFragment)
|
.replace(FRAGMENT_CONTAINER_ID, nativeFragment)
|
||||||
.commit()
|
.commitNow()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isFragmentCorrupted(fragment: BrowserFragment): Boolean {
|
||||||
|
if (!fragment.isAdded || fragment.isDetached || fragment.isRemoving) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
val view = fragment.view ?: return true
|
||||||
|
if (view.visibility != View.VISIBLE || view.width == 0 || view.height == 0) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
override fun onTrimMemory(level: Long) {
|
override fun onTrimMemory(level: Long) {
|
||||||
requireNotNull(GlobalComponents.components) { "Components not initialized" }
|
requireNotNull(GlobalComponents.components) { "Components not initialized" }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user