What is the meaning of addToBackStack with null parameter?

后端 未结 3 1366
渐次进展
渐次进展 2020-11-29 18:22

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 follow

3条回答
  •  情深已故
    2020-11-29 18:50

    The tag string 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):

    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.

    In other words, it will pop your back stack until it finds the fragment that was added by the name in 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).

    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().

提交回复
热议问题