Android DialogFragment偶发不能清除的问题

本文探讨了使用DialogFragment实现对话框显示与关闭时遇到的问题,特别是在调用dismiss方法时偶尔出现无法正常关闭的情况。通过分析源码,指出使用commitAllowingStateLoss()代替commit()可以避免状态丢失导致的问题,并推荐在适当情况下调用executePendingTransactions()以确保立即执行交易。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对话框现在多用DialogFragment来代替Dialog,在关闭对话框时需要dismiss操作;但如下代码偶发不能dismiss的问题:使用在progress dialog中,发送请求是show,请求完成后dismiss。


public static void show(FragmentManager manager) {
		DiyDialog dialogFragment = new DiyDialog();
		try {
			if (!dialogFragment.isAdded()) {
				FragmentTransaction fragmentTransaction=manager.beginTransaction();
				fragmentTransaction.add(dialogFragment, TAG);
				fragmentTransaction.commitAllowingStateLoss();

				//manager.executePendingTransactions();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressLint("NewApi")
	public static void closeDialog(FragmentManager manager) {
		DiyDialog dialogFragment = (DiyDialog) manager.findFragmentByTag(TAG);
		if (dialogFragment != null) {
			dialogFragment.dismissAllowingStateLoss();
		}
	}


单步调试时发现,在调用closeDialog时,不能找到此dialogFragment,因此不能正确的做dismiss操作。明明已经show了,怎么找不到呢?查看源码DOC:

/**
     * Schedules a commit of this transaction.  The commit does
     * not happen immediately; it will be scheduled as work on the main thread
     * to be done the next time that thread is ready.
     *
     * <p class="note">A transaction can only be committed with this method
     * prior to its containing activity saving its state.  If the commit is
     * attempted after that point, an exception will be thrown.  This is
     * because the state after the commit can be lost if the activity needs to
     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
     * situations where it may be okay to lose the commit.</p>
     * 
     * @return Returns the identifier of this transaction's back stack entry,
     * if {@link #addToBackStack(String)} had been called.  Otherwise, returns
     * a negative number.
     */
    public abstract int commit();

    /**
     * Like {@link #commit} but allows the commit to be executed after an
     * activity's state is saved.  This is dangerous because the commit can
     * be lost if the activity needs to later be restored from its state, so
     * this should only be used for cases where it is okay for the UI state
     * to change unexpectedly on the user.
     */
    public abstract int commitAllowingStateLoss();

大概的意思是:安排了一个commit操作,但不会立刻执行。而如下的这个还是也进一步说明了:commit操作被安排在主线程异步执行。如果想要立即执行,执行如下函数将会有效。

/**
     * After a {@link FragmentTransaction} is committed with
     * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
     * is scheduled to be executed asynchronously on the process's main thread.
     * If you want to immediately executing any such pending operations, you
     * can call this function (only from the main thread) to do so.  Note that
     * all callbacks and other related behavior will be done from within this
     * call, so be careful about where this is called from.
     *
     * @return Returns true if there were any pending transactions to be
     * executed.
     */
    public abstract boolean executePendingTransactions();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值