Created
October 7, 2020 05:03
-
-
Save AmaldevTA/ffd47059d194fcc591fdf06d3fa9ecdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
supportFragmentManager.commit { | |
replace(binding.container.id, MyFragment::class.java, bundleOf( | |
"KEY" to "userId" | |
)) | |
addToBackStack(null) | |
} | |
FragmentTransaction ft = supportFragmentManager.beginTransaction() | |
ft.replace(binding.container.id, MyFragment.newInstance("userId")) | |
ft.addToBackStack(null) | |
ft.commit() | |
NavHostFragment.findNavController(this) | |
.navigate(R.id.action_homFragment_to_myFragment, bundleOf( | |
"KEY" to "userId" | |
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val viewModel: MyViewModel by viewModels() | |
val viewModel = ViewModelProvider(owner).get(MyViewModel::class.java) | |
companion object{ | |
fun newInstance(userId: String?): MyFragment { | |
val fragment = MyFragment() | |
fragment.arguments = bundleOf( | |
"KEY" to "userId" | |
) | |
return fragment | |
} | |
} | |
arguments?.let { | |
userId = it.getString("KEY") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requireActivity().getOnBackPressedDispatcher().addCallback(onBackPressedCallback) | |
onBackPressedCallback.remove() | |
class MyFragment : Fragment(), FragmentManager.OnBackStackChangedListener { | |
override fun onBackStackChanged() { | |
//....... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment