Ignoring navigate() call: FragmentManager has already saved its state

前端 未结 5 2233
逝去的感伤
逝去的感伤 2021-02-08 01:19

I\'m using navigation in MainActivity, then I start SecondActivity (for result). After finish of SecondActivity I would like to continue w

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-08 02:07

    I believe above solutions should work. But my problem was different. There was a third party sdk which was launching its activity using context provided by me and it was delivering the result on a listener which I had to implement.

    So there was no option for me to work with onActivityResult :(

    I used below hack to solve the issue:

       private var runnable: Runnable? = null // Runnable object to contain the navigation code
    
        override fun onResume() {
            super.onResume()
    
            // run any task waiting for this fragment to be resumed
            runnable?.run()
        }
    
    
        override fun responseListener(response: Response) {    // Function in which you are getting response
    
            if (!isResumed) {    
                // add navigation to runnable as fragment is not resumed
                runnable = Runnable {
                    navController.navigate(R.id.destination_to_navigate)
                }
            } else {
                // navigate normally as fragment is already resumed
                navController.navigate(R.id.destination_to_navigate)
            }
        }
    
    
    

    Let me know if there is any better solution for this. Currently I found this very simple and easy to implement :)

提交回复
热议问题