Alert Dialog确认按钮异常:点击放弃修改无法返回主页
问题修复:点击「放弃修改」未返回主页
问题根源
你的MainActivity中removeEditPane方法调用了未实现的removeFragment函数,导致编辑页的AddEditFragment没有被正确移除,因此点击确认弹窗的「放弃修改」后,仅弹窗关闭,编辑页仍停留在界面上。
修复方案
方案1:实现removeFragment方法
在MainActivity中添加以下方法,确保正确移除目标Fragment:
private fun removeFragment(fragment: Fragment) { supportFragmentManager.beginTransaction() .remove(fragment) .commit() }
方案2:直接在removeEditPane中使用Fragment事务
修改removeEditPane方法,将未实现的removeFragment调用替换为直接的Fragment事务:
private fun removeEditPane(fragment: Fragment? = null) { Log.d(TAG, "removeEditPane called") if (fragment != null) { // 替换原removeFragment调用为以下代码 supportFragmentManager.beginTransaction() .remove(fragment) .commit() } // 原有逻辑保持不变 task_details_container.visibility = if (mTwoPane) View.INVISIBLE else View.GONE mainFragment.visibility = View.VISIBLE supportActionBar?.setDisplayHomeAsUpEnabled(false) }
验证逻辑
修复后,当你点击「放弃修改」时:
AppDialog会触发MainActivity的onPositiveDialogResult回调- 回调中调用
removeEditPane,正确移除编辑Fragment并切换显示主页 - 最终实现返回主页的预期效果
内容的提问来源于stack exchange,提问作者Muhab Al-Afandi
相关产品推荐
相关产品推荐

