Pass data/bundle using navigateUp in Android Navigation Component

前端 未结 6 1123
予麋鹿
予麋鹿 2020-12-14 09:51

I found the question but does not have solution in code

I want to have data when backpress/manual back happens. I am using navigateUp() to go back. How

6条回答
  •  悲哀的现实
    2020-12-14 10:13

    You should use static variables/companion objects, because it is better than shared viewmodel as it is not simple/nice architecture. As it it not straightforward, I think it is the best way.

    To navigateUp From FragmentB to FragmentA

    FragmentB:

    isBackpressed = true
    findNavController().navigateUp() 
    

    FragmentA:

    onViewCreated() {
        // todo
        if(isBackpressed) {
             isBackpressed = false
             // do whatever you want
        }
    }
    

提交回复
热议问题