I have an app with an mix of different architectural patterns that appear to be conflicting. It consists of an activity with a custom override for the back press
class MyActivity : BaseActivity(){
override fun onBackPressed() {
supportFragmentManager.apply {
if (backStackEntryCount == 1) {
finish()
} else if (this.findFragmentByTag("MyFragment")?.isVisible == true) {
val fragment = this.findFragmentByTag("MyFragment") as MyFragment
fragment.onBackPressed()
} else {
if (backStackEntryCount > 1) {
popBackStack()
} else {
super.onBackPressed()
}
}
}
}
}
That method contains a check for a particular fragment:
class MyFragment : BaseFragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = ComposeView(requireContext()).apply {
setContent {
MyComposable()
}
}
}
fun onBackPressed(){
Toast.makeText(context,"we've overridden the back button", Toast.LENGTH_LONG).show()
}
}
Which houses a composable with its own nav controller:
@Composable
fun MyComposable(
navController: NavHostController = rememberNavController(),
moduleNavigationType: ModuleNavigationType = ModuleNavigationType.MINIMAL
) {
Scaffold() { innerPadding ->
NavHost(
navController = navController,
startDestination = MarketplaceScreen.Start.route,
modifier = Modifier.padding(innerPadding)
) {
composable(route = MyComposable.Start.route) {
MyScreenA(
navController = navController,
creditAuthClient = creditAuthClient,
analyticsHelper = analyticsHelper
)
}
composable(route = MyComposable.CategoryDeals.route) {
MyScreenB(
navController = navController,
creditAuthClient,
analyticsHelper = analyticsHelper,
moduleNavigationType = moduleNavigationType)
}
}
}
}
I am trying to control the back navigation of the composable (going back to MyScreenA from MyScreenB or vice versa) using the system back button. With the check for the visibility of MyFragment in MyActivity, I am able to give the fragment custom back functionality, but I am not sure how to pass control of the system back button down further into MyComposable. The nature of the project may make it impossible to refactor either the