了解片段事务期间片段的生命周期方法调用

Understanding Fragment's lifeCycle methods calls during fragment transaction

我创建了一个演示,以了解在片段的不同情况下调用了哪些所有片段生命周期的方法 transaction.While 大多数调用都符合预期,我仍然感到困惑的是我用粗体写的一些东西。

假设有两个分片A和B,我们在它们之间进行交易

案例一

当片段 B 添加到片段 A 时

getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container, fragementB).addToBackStack(null).commit();

片段 B

onAttach

onCreate

onCreateView

onActivityCreated

onStart

onResume

没有调用片段 A 的生命周期方法。

我期望的是什么?

由于片段A不可见,调用了片段A的onStop方法

根据文档-

Stopped- The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.

这是否意味着在同一片段中添加新片段时没有调用当前片段的方法activity?

然后在片段 B

中使用 popBackStack()

片段 B

onPause

onStop

onDestroyView

onDestroy

onDetach

没有调用片段 A 的生命周期方法

我期望的是什么?

Fragment A 的 onStart 方法被调用,因为 Fragment A 现在可见

案例二

当片段 B 替换片段 A

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).commit();

片段 B

onAttach

onCreate

onCreateView

onActivityCreated

onStart

onResume

片段A

onPause

onStop

onDestroyView

onDestroy

onDetach

一切如期而至

案例三

当 Fragment B 替换 Fragment A 时将其保留在后台堆栈中

 getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).addToBackStack("tag").commit();

片段 B

onAttach

onCreate

onCreateView

onActivityCreated

onStart

onResume

片段A

onPause

onStop

onDestroyView

Fragment A 的 onDestroy 和 onDetach 方法不是 called.Why 它没有被调用?Bcoz 根据文档方法 replace 删除容器中已经存在的任何片段并添加您的同一容器中的新容器

然后在片段 B

中使用 popBackStack()

片段A

onCreateView

onActivityCreated

onStart

onResume

片段 B

onPause

onStop

onDestroyView

onDestroy

onDetach

setUserVisibleHint 片段可见或不可见时调用

Does this mean that no method of current fragment is called when new fragment is added in same activity?

正确,您的第一个片段 A 只有在被删除或替换后才会受到影响(情况 2)。 简单地添加另一个片段只会在片段 A 上显示片段 B,并且不应调用任何生命周期回调。

What i expected was?

onStart method of Fragment A is called since Fragment A is visible now

同样,由于片段 B 是在 A 之上添加的,因此片段 A 不受删除 B 的影响。

onDestroy and onDetach method of Fragment A is NOT called.Why its not called?Bcoz as per documentation method replace removes any fragments that are already in the container and add your new one to the same container

与简单的替换不同,当您将替换事务添加到后台堆栈时,您实际上是将第一个片段附加到它 activity,只有它的视图被破坏。

弹出后退堆栈片段 B 后,片段 A 将重新创建其视图 - 从 onCreateView() 开始。

我运行一些日志看结果如下(就像在活动这里做的:

片段开始

On Fragment Launched (First Time)
———————————————————————
onAttach: 
onCreateView:
onViewCreated: 
onActivityCreated: 
onStart: 
onResume: 


On Coming Back To Fragment (From another fragment)
———————————————————————
onCreateView: 
onViewCreated: 
onActivityCreated: 
onStart: 
onResume: 


OnMaximize(Square Button-After Back Pressed)
———————————————————————
onAttach: 
onCreateView: 
onViewCreated: 
onActivityCreated: 
onStart: 
onResume: 


OnMaximize(Square Button-After Circle Button)
———————————————————————
onStart: 
onResume:


OnMaximize(After Circle Button)
————————————————————————————————————————————————
onStart: 
onResume:

片段已停止

On Going To Another Fragment (Skipping 1 Fragment)
———————————————————————
onPause: 
onStop: 
onDestroyView: 


On BackPressed - Reverse Triangle Button (App Minimized)
———————————————————————
onPause: 
onStop: 
onDestroyView: 
onDestroy: 
onDetach: 


OnMinimize (Circle Button)
————————————————————————————————————————————————
onPause: 
onStop: 


OnMinimize (Square Button)
————————————————————————————————————————————————
onPause: 
onStop: 


Going To Another Activity
————————————————————————————————————————————————
onPause: 
onStop: 


Close The App
————————————————————————————————————————————————
onDestroyView: 
onDestroy:

如果有人想重新创建检查以下是代码:

public View onCreateView(@NonNull LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    Log.d("TAG",
          "onCreateView: ");
}

@Override
public void onAttach(Context context) {
    Log.d("TAG",
          "onAttach: ");
    super.onAttach(context);
}

@Override
public void onViewCreated(@NonNull View view,
                          @Nullable Bundle savedInstanceState) {

    Log.d("TAG",
          "onViewCreated: ");
    super.onViewCreated(view,
                        savedInstanceState);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    Log.d("TAG",
          "onActivityCreated: ");
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onStart() {
    Log.d("TAG",
          "onStart: ");
    super.onStart();
}

@Override
public void onResume() {

    Log.d("TAG",
          "onResume: ");

    reAttachListeners();
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();

    Log.d("TAG",
          "onPause: ");
    removeListeners();

}

@Override
public void onStop() {
    Log.d("TAG",
          "onStop: ");
    super.onStop();
}

@Override
public void onDestroyView() {
    Log.d("TAG",
          "onDestroyView: ");
    super.onDestroyView();
}

@Override
public void onDestroy() {
    Log.d("TAG",
          "onDestroy: ");
    super.onDestroy();
}

@Override
public void onDetach() {

    Log.d("TAG",
          "onDetach: ");
    super.onDetach();
}