新的FragmentTransaction commitNow()在内部如何工作? [英] How is the new FragmentTransaction commitNow() working internally?

查看:124
本文介绍了新的FragmentTransaction commitNow()在内部如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的 commitNow()在Android N和支持库版本24中添加的方法的文档有限且有点混乱.

The new commitNow() method added in Android N and support library version 24 has a limited and a bit confusing documentation.

同步提交此事务.任何添加的片段将是 初始化并完全进入其生命周期状态 主机和所有删除的碎片将在此之前被相应拆除 此电话返回.以这种方式提交交易允许 作为专用的封装组件添加的片段 监控主机的生命周期状态,同时提供更牢固的支持 这些片段完全初始化时的排序保证 准备好了管理视图的片段将创建这些视图 并附上.

Commits this transaction synchronously. Any added fragments will be initialized and brought completely to the lifecycle state of their host and any removed fragments will be torn down accordingly before this call returns. Committing a transaction in this way allows fragments to be added as dedicated, encapsulated components that monitor the lifecycle state of their host while providing firmer ordering guarantees around when those fragments are fully initialized and ready. Fragments that manage views will have those views created and attached.

调用commitNow比调用commit()更可取 其次是FragmentManager.executePendingTransactions()作为后者 将会产生尝试当前提交所有操作的副作用 挂起的交易,无论这是否是所需的行为.

Calling commitNow is preferable to calling commit() followed by FragmentManager.executePendingTransactions() as the latter will have the side effect of attempting to commit all currently pending transactions whether that is the desired behavior or not.

以这种方式提交的交易可能不会添加到 FragmentManager的后堆栈,因为这样做会破坏其他预期 其他异步提交的事务的排序保证. 如果交易,此方法将抛出IllegalStateException 以前要求将其添加到后堆栈中 addToBackStack(String).

Transactions committed in this way may not be added to the FragmentManager's back stack, as doing so would break other expected ordering guarantees for other asynchronously committed transactions. This method will throw IllegalStateException if the transaction previously requested to be added to the back stack with addToBackStack(String).

只能使用此提交事务 方法在其包含活动之前保存其状态.如果 在此之后尝试提交,将引发异常. 这是因为如果活动导致提交后的状态可能会丢失 需要从其状态中恢复.参见commitAllowingStateLoss() 可能会丢失提交的情况.

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 commitAllowingStateLoss() for situations where it may be okay to lose the commit.

我以粗体突出显示了我认为令人困惑的部分.

I have highlighted in bold the part that i think it is confusing.

所以,我主要关心的问题是:

So, my main concerns/questions are:

1-可能不添加?它说我将收到一个IllegalStateException,所以会添加还是不添加?

1 - They MAY NOT be added? It says i will get an IllegalStateException, so will it be or will not be added?

2-我接受以下事实:如果我们想在后堆栈中添加一个片段,则无法使用它.它不是说您收到此异常:

2 - I accept the fact that I cannot use this if we want to add a fragment in the backstack. What it doesn't say is that you get this exception:

java.lang.IllegalStateException: This transaction is already being added to the back stack

!!!! ????

!!!!????

因此我无法自己呼叫addToBackStack(String),因为它是在内部为我调用的?对不起,但是...什么?为什么?如果我不希望将其添加到堆栈中怎么办?而且,如果我稍后尝试从后向堆栈中使用该片段,但又由于可能无法添加而导致该片段不存在,该怎么办?

So i cannot call addToBackStack(String) myself because it is internally calling it for me? I am sorry but... what? why? what if i don't want it to be added in the backstack? And what if I try to use that fragment from the backstack later but because it MAY NOT be added, later it is not there?

如果我使用的是commitAllowingStateLoss(),这似乎是预料之中的,但是我看到commitNowAllowingStateLoss()也存在,所以...它遵循哪种逻辑?

It looks like this is something expected if i was using commitAllowingStateLoss(), but i see that commitNowAllowingStateLoss() also exists, so... what kind of logic does it follow?

TL; DR

有关backstack的commitNow()在内部如何工作?

How is commitNow() working internally regarding the backstack?

推荐答案

当我们遇到这样的问题时,Android源代码是开源是一件好事!

It's a good thing that Android Source code is Open Source when we faced some question like this!

因此,让我们看一下BackStackRecord源此处

So let's take a look at BackStackRecord source here

@Override
public void commitNow() {
    disallowAddToBackStack();
    mManager.execSingleAction(this, false);
}

@Override
public FragmentTransaction disallowAddToBackStack() {
    if (mAddToBackStack) {
        throw new IllegalStateException(
                "This transaction is already being added to the back stack");
    }
    mAllowAddToBackStack = false;
    return this;
}

如果在交易中调用addToBackStack,则mAddToBackStack将设置为true.

And mAddToBackStack will be set to true if you call addToBackStack in your transaction.

因此,要回答您的问题,在调用commitNow()时不会在内部调用任何addToBackStack,这是异常消息,含义不明确.我认为应该说You're not allowed to add to backstack when using commitNow()而不是当前消息.

So to answer your question, no addToBackStack isn't called internally when you call commitNow(), It's the exception message that ambigous. I think it should say You're not allowed to add to backstack when using commitNow() instead the current message.

如果我们深入研究FragmentManager源代码此处commitNow()实际上与上述executePendingTransactions()几乎相同的操作,但是执行所有先前提交的事务时,commitNow()只会提交交易.

If we dig deeper into FragmentManager source code here, commitNow() actually doing almost same thing as executePendingTransactions() like written above, but instead executing all previously committed transaction, commitNow() will only commit that transaction.

我认为这是commitNow()不允许向后堆栈添加内容的主要原因,因为它不能保证没有任何其他挂起的事务.如果commitNow()可以添加到Backstack中,则有可能我们会破坏Backstack序列,这将导致意外的情况.

I think that's the main reason why commitNow() isn't allowing addition to the backstack since it cannot guarantee there aren't any other pending transaction. If commitNow() can add to the backstack, there is a possibility that we can break our backstack sequence that will leading into unexpected thing.

这篇关于新的FragmentTransaction commitNow()在内部如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆