Open naozumi-nao opened 1 year ago
Currently, one of the ways to avoid this issue altogether is to use Toast instead of AlertDialog
private suspend fun addUserToCompany() {
val userId = binding?.edUserIdInput?.text.toString()
val userRole = when (binding?.actvChooseUserRole?.text.toString()) {
"MANAGER" -> User.UserRole.MANAGER
"EMPLOYEE" -> User.UserRole.EMPLOYEE
else -> User.UserRole.EMPLOYEE
}
user = viewModel.getUserData(userId)
viewModel.addUserToCompany(companyId, user, userRole)
.observe(this) { result ->
if (result != null) {
when (result) {
is Result.Loading -> {
binding?.progressBar?.visibility = View.VISIBLE
}
is Result.Success -> {
binding?.progressBar?.visibility = View.GONE
Toast.makeText(
requireActivity(),
"Successfully added user to company",
Toast.LENGTH_LONG
).show()
dismiss()
ViewUtils.replaceFragment(
requireActivity() as AppCompatActivity,
R.id.nav_main_content_container,
CompanyProfileFragment(),
CompanyProfileFragment::class.java.simpleName
)
}
is Result.Error -> {
binding?.progressBar?.visibility = View.GONE
Toast.makeText(
requireActivity(),
"Error: " + result.error,
Toast.LENGTH_LONG
).show()
}
}
}
}
}
dismiss() will cause the DialogFragment in question get unattached from the parent fragment, which in turn makes AlertDialog to lose its context. After that it's going to either: