Skip to content

Commit

Permalink
fix: ignore IllegalStateException when fragment transaction fails (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
dixidroid authored Oct 7, 2024
1 parent a86d95d commit 42f5835
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions app/src/main/java/org/openedx/app/AppRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ class AppRouter : AuthRouter, DiscoveryRouter, DashboardRouter, CourseRouter, Di
infoType: String?,
openTab: String
) {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, MainFragment.newInstance(courseId, infoType, openTab))
.commit()
try {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, MainFragment.newInstance(courseId, infoType, openTab))
.commit()
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun navigateToSignIn(fm: FragmentManager, courseId: String?, infoType: String?) {
Expand Down Expand Up @@ -102,18 +106,26 @@ class AppRouter : AuthRouter, DiscoveryRouter, DashboardRouter, CourseRouter, Di
}

override fun navigateToWhatsNew(fm: FragmentManager, courseId: String?, infoType: String?) {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, WhatsNewFragment.newInstance(courseId, infoType))
.commit()
try {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, WhatsNewFragment.newInstance(courseId, infoType))
.commit()
} catch (e: Exception) {
e.printStackTrace()
}
}

override fun clearBackStack(fm: FragmentManager) {
fm.apply {
for (fragment in fragments) {
beginTransaction().remove(fragment).commit()
try {
for (fragment in fragments) {
beginTransaction().remove(fragment).commit()
}
popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
} catch (e: Exception) {
e.printStackTrace()
}
popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
}
}
//endregion
Expand Down Expand Up @@ -424,29 +436,41 @@ class AppRouter : AuthRouter, DiscoveryRouter, DashboardRouter, CourseRouter, Di
}

private fun replaceFragmentWithBackStack(fm: FragmentManager, fragment: Fragment) {
fm.beginTransaction()
.replace(R.id.container, fragment, fragment.javaClass.simpleName)
.addToBackStack(fragment.javaClass.simpleName)
.commit()
try {
fm.beginTransaction()
.replace(R.id.container, fragment, fragment.javaClass.simpleName)
.addToBackStack(fragment.javaClass.simpleName)
.commit()
} catch (e: Exception) {
e.printStackTrace()
}
}

private fun replaceFragment(
fm: FragmentManager,
fragment: Fragment,
transaction: Int = FragmentTransaction.TRANSIT_NONE,
) {
fm.beginTransaction()
.setTransition(transaction)
.replace(R.id.container, fragment, fragment.javaClass.simpleName)
.commit()
try {
fm.beginTransaction()
.setTransition(transaction)
.replace(R.id.container, fragment, fragment.javaClass.simpleName)
.commit()
} catch (e: Exception) {
e.printStackTrace()
}
}

//App upgrade
override fun navigateToUserProfile(fm: FragmentManager) {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, ProfileFragment())
.commit()
try {
fm.popBackStack()
fm.beginTransaction()
.replace(R.id.container, ProfileFragment())
.commit()
} catch (e: Exception) {
e.printStackTrace()
}
}
//endregion
}

0 comments on commit 42f5835

Please sign in to comment.