Difference between add(), replace(), and addToBackStack()

后端 未结 9 1587
终归单人心
终归单人心 2020-11-22 05:40

What is the main difference between calling these methods:

fragmentTransaction.addToBackStack(name);
fragmentTransaction.replace(containerViewId, fragment, t         


        
9条回答
  •  遥遥无期
    2020-11-22 06:16

    Although it is an old question already answered, maybe those next examples can complement the accepted answer and they can be useful for some new programmers in Android as I am.

    Option 1 - "addToBackStack()" is never used

    Case 1A - adding, removing, and clicking Back button

    Activity :      onCreate() - onStart() - onResume()                             Activity is visible
    add Fragment A :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment A is visible
    add Fragment B :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment B is visible
    add Fragment C :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment C is visible
    remove Fragment C :     onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Fragment B is visible
    (Back button clicked)
    Activity :      onPause() - onStop() - onDestroy()
    Fragment A :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
    Fragment B :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               App is closed, nothing is visible
    

    Case 1B - adding, replacing, and clicking Back button

    Activity :      onCreate() - onStart() - onResume()                             Activity is visible
    add Fragment A :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment A is visible
    add Fragment B :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment B is visible
    (replace Fragment C)    
    Fragment B :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               
    Fragment A :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()
    Fragment C :        onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment C is visible
    (Back button clicked)
    Activity :      onPause() - onStop() - onDestroy()
    Fragment C :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               App is closed, nothing is visible
    

    Option 2 - "addToBackStack()" is always used

    Case 2A - adding, removing, and clicking Back button

    Activity :      onCreate() - onStart() - onResume()                             Activity is visible
    add Fragment A :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment A is visible
    add Fragment B :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment B is visible
    add Fragment C :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment C is visible
    remove Fragment C :     onPause() - onStop() - onDestroyView()                              Fragment B is visible
    (Back button clicked)
    Fragment C :        onCreateView() - onActivityCreated() - onStart() - onResume()                   Fragment C is visible
    (Back button clicked)
    Fragment C :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Fragment B is visible
    (Back button clicked)
    Fragment B :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Fragment A is visible
    (Back button clicked)
    Fragment A :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Activity is visible
    (Back button clicked)
    Activity :      onPause() - onStop() - onDestroy()                              App is closed, nothing is visible
    

    Case 2B - adding, replacing, removing, and clicking Back button

    Activity :      onCreate() - onStart() - onResume()                             Activity is visible
    add Fragment A :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment A is visible
    add Fragment B :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment B is visible
    (replace Fragment C)    
    Fragment B :        onPause() - onStop() - onDestroyView()  
    Fragment A :        onPause() - onStop() - onDestroyView() 
    Fragment C :        onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment C is visible
    remove Fragment C :     onPause() - onStop() - onDestroyView()                              Activity is visible
    (Back button clicked)
    Fragment C :        onCreateView() - onActivityCreated() - onStart() - onResume()                   Fragment C is visible
    (Back button clicked)
    Fragment C :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               
    Fragment A :        onCreateView() - onActivityCreated() - onStart() - onResume()   
    Fragment B :        onCreateView() - onActivityCreated() - onStart() - onResume()                   Fragment B is visible
    (Back button clicked)
    Fragment B :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Fragment A is visible
    (Back button clicked)
    Fragment A :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Activity is visible
    (Back button clicked)
    Activity :      onPause() - onStop() - onDestroy()                              App is closed, nothing is visible
    

    Option 3 - "addToBackStack()" is not used always (in the below examples, w/o indicates that it is not used)

    Case 3A - adding, removing, and clicking Back button

    Activity :      onCreate() - onStart() - onResume()                             Activity is visible
    add Fragment A :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment A is visible
    add Fragment B w/o:     onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment B is visible
    add Fragment C w/o:     onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment C is visible
    remove Fragment C :     onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Fragment B is visible
    (Back button clicked)
    Fragment B :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               
    Fragment A :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Activity is visible
    (Back button clicked)
    Activity :      onPause() - onStop() - onDestroy()                              App is closed, nothing is visible
    

    Case 3B - adding, replacing, removing, and clicking Back button

    Activity :      onCreate() - onStart() - onResume()                             Activity is visible
    add Fragment A :    onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment A is visible
    add Fragment B w/o:     onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment B is visible
    (replace Fragment C)    
    Fragment B :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()   
    Fragment A :        onPause() - onStop() - onDestroyView() 
    Fragment C :        onAttach() - onCreate() - onCreateView() - onActivityCreated() - onStart() - onResume()     Fragment C is visible
    remove Fragment C :     onPause() - onStop() - onDestroyView()                              Activity is visible
    (Back button clicked)
    Fragment C :        onCreateView() - onActivityCreated() - onStart() - onResume()                   Fragment C is visible
    (Back button clicked)
    Fragment C :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               
    Fragment A :        onCreateView() - onActivityCreated() - onStart() - onResume()                   Fragment A is visible
    (Back button clicked)
    Fragment A :        onPause() - onStop() - onDestroyView() - onDestroy() - onDetach()               Activity is visible
    (Back button clicked)
    Activity :      onPause() - onStop() - onDestroy()                              App is closed, nothing is visible
    

提交回复
热议问题