android - Android警报对话框故障
问题描述
我无法弄清楚这种奇怪的行为。
这在某些设备上随机发生,并且总是在 Honor 9 STF-L9(gif 动画中的那个)上发生。
如果您查看此 gif,您会看到从选择器返回,对话框再次可见,即使没有人显示它(通过单击查看完整图像 gif 以查看视频)。
我有一个Activity
应用程序(我使用新的 Navigation jetpack 组件),并且我有一个自AlertDialog
定义MainActivity
:
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private lateinit var inflater: LayoutInflater
private lateinit var dialogView: View
private lateinit var messageTextView: TextView
private val progressHud: AlertDialog by lazy {
val dialogBuilder =
AlertDialog.Builder(this, R.style.ProgressHudTheme)
messageTextView.visibility = View.GONE
dialogBuilder.setView(dialogView)
dialogBuilder.setCancelable(false)
dialogBuilder.create()
}
fun showProgressHud(message: String?) {
messageTextView.visibility = View.GONE
message?.let {
messageTextView.text = it
messageTextView.visibility = View.VISIBLE
}
progressHud.show()
}
fun hideProgressHud() {
progressHud.hide()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.fab.setOnClickListener { view ->
dispatchChoosePictureFromGalleryAppIntentIntent()
}
setupProgressHud()
}
private fun dispatchChoosePictureFromGalleryAppIntentIntent() {
Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
).apply {
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
resolveActivity(packageManager)?.also {
requestImageChooserLauncher.launch(this)
}
}
}
private var requestImageChooserLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
val resultCode = result.resultCode
val data = result.data
// TODO ...
}
private fun setupProgressHud() {
inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
dialogView = inflater.inflate(R.layout.progress_hud, null)
messageTextView = dialogView.findViewById(R.id.progressHudTextView)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment_content_main)
return navController.navigateUp(appBarConfiguration)
|| super.onSupportNavigateUp()
}
}
对话框以这种方式显示和隐藏在FirstFragment
:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.buttonFirst.setOnClickListener {
(activity as? MainActivity)?.showProgressHud("Loading...")
Handler().postDelayed({
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
(activity as? MainActivity)?.hideProgressHud()
}, 5000)
}
}
你可以在这里找到一个完整的工作示例:https ://github.com/shadowsheep1/android-alert-dialog-glitch
我仍然不明白为什么会发生这种情况。
如果有人以前遇到过这种情况或有任何线索,将不胜感激。
解决方案
尝试更换
progressHud.hide()
经过
progressHud.dismiss().
那应该可以正常工作。
dismiss()
using over背后还有一个更重要的原因hide()
: usinghide()
可能导致Leaked Window Error,因为它并没有真正删除视图,只是隐藏它,然后如果你完成可能导致泄漏的活动。
推荐阅读
- java - Code optimization to covert list of integers to list of objects in Java
- react-native - 在 React Native 中没有粘性标题的 Flatlist Horizontal?
- php - Laravel Eloquent 获取存储在具有角色的类别中的所有帖子
- python - 未定义 Scipy 最小化名称“init_weigths”
- python - 无法在 Python 上安装 MMFashion
- swiftui - SwiftUI - 如何使用 onTapGesture 在 ScrollView 中仅初始化一个 SectionView
- r - ggplotly 在 RStudio 控制台中工作,但不能在闪亮的应用程序中呈现
- c# - 使用 c# 从所有组中删除单个用户,域用户除外?
- c - CLion - 未找到 SDL 库 Mac
- reactjs - 如何设计 React Slick carrousel 的箭头样式?