Android 带空参数的 addToBackStack 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22984950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:39:21  来源:igfitidea点击:

What is the meaning of addToBackStack with null parameter?

androidandroid-fragments

提问by My God

I have a customer code. There is only one activity for all of the fragments i.e. the single activity is managing all the fragments.

我有一个客户代码。所有片段只有一个活动,即单个活动正在管理所有片段。

This activity contains the following code for any fragment at the method end of that fragment-

此活动包含该片段方法末尾的任何片段的以下代码 -

For example - fragment MoreFragment:

例如 - 片段 MoreFragment:

MoreFragment firstFragment = new MoreFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.addToBackStack(null).commit();

So,

所以,

1) What is the meaning of addToBackStack(null)followed by a commit()?

1)addToBackStack(null)后跟 a是什么意思commit()

2) Why you need to pass a null parameter to addToBackStack?

2)为什么需要向 传递一个空参数addToBackStack

3) How to get that fragment after being added like this ?

3)像这样添加后如何获取该片段?

Seems like this code is useless as I ran the code without the last line .addToBackStack(null).commit()and it ran without any problems.

似乎这段代码没用,因为我在没有最后一行的情况下运行了代码.addToBackStack(null).commit()并且它运行没有任何问题。

回答by Raghunandan

What is the meaning of addToBackStack(null) followed by a commit()?

addToBackStack(null) 后跟一个 commit() 是什么意思?

Quoting docs:

引用文档:

By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.

If you add multiple changes to the transaction (such as another add() or remove()) and call addToBackStack(), then all changes applied before you call commit() are added to the back stack as a single transaction and the Back button will reverse them all together.

通过调用 addToBackStack(),替换事务被保存到返回堆栈中,因此用户可以通过按下返回按钮来反转事务并返回前一个片段。

如果向事务添加多个更改(例如另一个 add() 或 remove())并调用 addToBackStack(),则在调用 commit() 之前应用的所有更改都将作为单个事务添加到返回堆栈中,并且返回按钮将它们全部反转。

The order in which you add changes to a FragmentTransaction doesn't matter, except:

向 FragmentTransaction 添加更改的顺序无关紧要,除了:

You must call commit()last. If you're adding multiple fragments to the same container, then the order in which you add them determines the order they appear in the view hierarchy.

你必须commit()最后打电话。如果您将多个片段添加到同一个容器,则添加它们的顺序决定了它们在视图层次结构中的显示顺序。

So you have to commit at the last.

所以你必须在最后承诺。

Why you need to pass a null parameter to addToBackStack?

为什么需要向 addToBackStack 传递一个空参数?

It don't need to be null, it can be a string. If you don't want, just pass null.

它不需要为空,它可以是一个字符串。如果您不想要,只需传递 null。

public abstract FragmentTransaction addToBackStack (String name)

Added in API level 11 Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Parameters name An optional name for this back stack state, or null.

公共抽象 FragmentTransaction addToBackStack(字符串名称)

在 API 级别 11 中添加 将此事务添加到后台堆栈。这意味着事务在提交后将被记住,并在稍后弹出堆栈时反转其操作。

参数 name 此返回堆栈状态的可选名称,或 null。

Concerning:

关于:

Seems like this code is useless as I ran the code without the last line .addToBackStack(null).commit() and it ran without any problems

似乎这段代码没用,因为我在没有最后一行 .addToBackStack(null).commit() 的情况下运行代码并且它运行没有任何问题

If you want to navigate to previous fragment add it to backstack. So it depends on whether you want to add the fragment to the backstack.

如果您想导航到上一个片段,请将其添加到 backstack。所以这取决于您是否要将片段添加到后台堆栈中。

How to get that fragment after being added like this?

像这样添加后如何获取该片段?

You already have the fragment instance firstFragment. So I don't know what you mean by get the fragment later.

您已经拥有片段实例firstFragment。所以我不知道你说稍后获取片段是什么意思。

More information @

更多信息 @

http://developer.android.com/guide/components/fragments.html

http://developer.android.com/guide/components/fragments.html

http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)

http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)

回答by Allan Veloso

The tagstring in addToBackStack(String name)gives a way to locate the back stack for later pop directly to that location. It meant to be used in the method popToBackStack(String name, int flags):

tag字符串addToBackStack(String name)提供了一种方式来确定背面堆栈后直接弹出到该位置。它意味着在方法中使用popToBackStack(String name, int flags)

Pop the last fragment transition from the manager's fragment back stack. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

name: If non-null, this is the name of a previous back state to look for; if found, all states up to that state will be popped. The POP_BACK_STACK_INCLUSIVE flag can be used to control whether the named state itself is popped. If null, only the top state is popped.

flags: Either 0 or POP_BACK_STACK_INCLUSIVE.

从管理器的片段返回堆栈中弹出最后一个片段转换。这个函数是异步的——它将请求排入队列以弹出,但直到应用程序返回到其事件循环才会执行该操作。

name:如果非空,这是要查找的先前返回状态的名称;如果找到,将弹出该状态之前的所有状态。POP_BACK_STACK_INCLUSIVE 标志可用于控制是否弹出命名状态本身。如果为 null,则仅弹出顶部状态。

标志:0 或 POP_BACK_STACK_INCLUSIVE。

In other words, it will pop your back stack until it finds the fragment that was added by the namein addToBackStack(String name).

换句话说,它会弹出您的返回堆栈,直到找到由namein添加的片段addToBackStack(String name)

For example, if you do a series of additions or replaces to the fragment manager giving the names "frag1", "frag2", "frag3", "frag4" and later want to go back directly to the fragment 2 added with addToBackStack("frag2"), you call popToBackStack("frag2", 0).

例如,如果您对片段管理器进行了一系列添加或替换,命名为“frag1”、“frag2”、“frag3”、“frag4”,然后想直接返回到使用 addToBackStack(" frag2"),您调用popToBackStack("frag2", 0).

So,

所以,

  • Use .addToBackStack("fragName"): if you want later popToBackStack(String name, int flags)to pop more than one back stack.

  • Use .addToBackStack(null): If you don't want later pop more than one back stack, but still want to pop one at a time. Do this even if you won't explicitly call popToBackStack() but instead will let the back press default implementation handle the back stack.

  • Use .disallowAddToBackStack(): If you don't want either the back press or call popBackStack() it explicitly. It will make sure no part of the code is using .addToBackStack().

  • 使用.addToBackStack("fragName"):如果您想稍后popToBackStack(String name, int flags)弹出多个返回堆栈。

  • 用途.addToBackStack(null):如果您不想稍后弹出多个返回堆栈,但仍希望一次弹出一个。即使您不会显式调用 popToBackStack() 而是让 back press 默认实现处理 back stack,也要这样做。

  • 使用.disallowAddToBackStack():如果您不想要后退或显式调用 popBackStack() 。它将确保代码的任何部分都没有使用 .addToBackStack()。

回答by EliaszKubala

Your answers are deprecated. If you don't want to add fragments to back stack, you should use below snippet of code:

您的答案已被弃用。如果您不想将片段添加到后台堆栈,则应使用以下代码片段:

    public static void replaceFragment (@NonNull FragmentManager fragmentManager,
            @NonNull Fragment fragment, int frameId){

        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(frameId, fragment);
        transaction.disallowAddToBackStack(); // <-- This makes magic!
        transaction.commit();
    }

Below you have cuteexample of how use it:

下面是如何使用它的可爱示例:

GameFragment fragment = GameFragment.newInstance(mGameObject, currentQuestion);
ActivityUtils.replaceFragment(getFragmentManager(), fragment, R.id.main);