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
class MyViewModel : ViewModel(), ... { | |
fun onSomeAction() { | |
viewModelScope.launch { | |
... | |
val dialogResult = showTryAgainDialog(data = ...) | |
... | |
} | |
} | |
} |
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
class HomeFragment : Fragment(R.layout.home_fragment) { | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
findNavController() | |
.currentBackStackEntry | |
?.savedStateHandle | |
?.getLiveData<String>("my-result-key") | |
?.observe(viewLifecycleOwner) { result -> |
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
class MyDialogFragment : DialogFragment() { | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
AlertDialog.Builder(requireContext()) | |
.setMessage(getString(R.string.order_confirmation)) | |
.setPositiveButton(getString(R.string.ok)) { _, _ -> | |
findNavController() | |
.previousBackStackEntry | |
?.savedStateHandle?.set("my-result-key", "example-data") | |
} |
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
class MainActivity : FragmentActivity(), | |
NoticeDialogFragment.NoticeDialogListener, | |
SecondDialogFragment.Listener, | |
ThirdDialogFragment.Listener { | |
private val viewModel by ... | |
fun showNoticeDialog() { | |
val dialog = NoticeDialogFragment() | |
dialog.show(supportFragmentManager, "NoticeDialogFragment") |
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
class NoticeDialogFragment : DialogFragment() { | |
internal lateinit var listener: NoticeDialogListener | |
interface NoticeDialogListener { | |
fun onDialogPositiveClick(dialog: DialogFragment) | |
fun onDialogNegativeClick(dialog: DialogFragment) | |
} | |
override fun onAttach(context: Context) { | |
super.onAttach(context) |
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
class MyFragment : Fragment(R.layout.my_layout) { | |
fun onSomeAction() { | |
coroutineScope.launch { | |
// Code A | |
when (val dialogResult = showDialogAndAwaitResult()) { | |
DialogResult.Ok -> { | |
// Code C | |
} |
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
class MyFragment : Fragment(R.layout.my_layout) { | |
fun onSomeAction() { | |
// Code A | |
showDialog( | |
onOkClicked = { ... }, | |
onCancelClicked = { ... } | |
) |
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
class HomeFragment : Fragment(R.layout.home_fragment) { | |
private val binding by viewBinding<HomeFragmentBinding>() | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
binding.button.setOnClickListener { | |
findNavController().navigate(HomeFragmentDirections.toMyDialog()) | |
} |