I'm using single activity architecture and have 3 fragments (Fragment A, Fragment B and Fragment C). Click on button in Fragment A takes to Fragment B and click on button in Fragment B takes to Fragment C (which is a webview, a game is running inside this) Now I want to go back to Fragment B and want the game to be paused and again when I come back to Fragment C from B, I want the Game to be in paused state
I'm using navigation component to navigate between the fragments.
When I use navigateUp() while going back from C to B, It destroys the fragment C and recreates it when navigating to C from B and the game starts fresh again.
Any suggestions how to handle this scenario using navigation component ?
You can achieve this by using ViewModel and onSaveInstanceState() to save and restore the state of your WebView and the game inside it.
This way, when you navigate back from Fragment C to Fragment B, the WebView and the game state will be saved in the ViewModel. When you navigate to Fragment C again, the WebView state will be restored, and the game will be in the paused state.
Note: You may need to modify the game code to pause/resume the game based on the WebView state if the game does not handle it automatically. Also, make sure to manage the WebView lifecycle (such as onPause, onResume, and onDestroy) in your Fragment C.