improve fragment initialization
This commit is contained in:
+11
-11
@@ -177,22 +177,22 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (activity == null || activity !is FragmentActivity) {
|
val fragmentActivity = activity as? FragmentActivity ?: return false
|
||||||
|
|
||||||
|
// Check if activity is in valid state
|
||||||
|
if (fragmentActivity.isFinishing || fragmentActivity.isDestroyed) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val fragmentActivity = activity as FragmentActivity
|
val container = fragmentActivity.findViewById<View>(FRAGMENT_CONTAINER_ID) ?: return false
|
||||||
|
|
||||||
// Check if the container view exists in the view hierarchy
|
|
||||||
val container = fragmentActivity.findViewById<View>(FRAGMENT_CONTAINER_ID)
|
|
||||||
if (container == null) {
|
|
||||||
// Container doesn't exist yet, retry later
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val fm = fragmentActivity.supportFragmentManager
|
val fm = fragmentActivity.supportFragmentManager
|
||||||
|
|
||||||
// Check if fragment already exists in the container
|
// Ensure FragmentManager is in valid state
|
||||||
|
if (fm.isStateSaved) {
|
||||||
|
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
|
// Fragment already replaced, no need to do it again
|
||||||
@@ -202,7 +202,7 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
|
|||||||
val nativeFragment = BrowserFragment.create()
|
val nativeFragment = BrowserFragment.create()
|
||||||
fm.beginTransaction()
|
fm.beginTransaction()
|
||||||
.replace(FRAGMENT_CONTAINER_ID, nativeFragment)
|
.replace(FRAGMENT_CONTAINER_ID, nativeFragment)
|
||||||
.commitAllowingStateLoss()
|
.commit()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user