“This is the 12th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

Whenever we change jobs before the interview, always involuntarily to brush up the interview questions, most of the questions we don’t know how to brush the repeated again, but remember that today, when the next interview and brush the same interview questions, then I would like to ask everyone here, the Activity of the life cycle, how many times you really painted cry [laughs] as a programmer It’s unbearable to waste time on repetitive tasks, so in order to save myself some unnecessary mental overhead, I’ve compiled a list of interviewing memory techniques that I’d like to share with you. Memorizing isn’t about making your knowledge your own

The premise

You need to be familiar with the Activity lifecycle to understand and remember the Fragment lifecycle by comparing the Activity lifecycle

The lifecycle of an Activity

Assuming you’re already familiar with the Activity lifecycle, let’s look at the Fragment lifecycle diagram

Life cycle of the Fragment

Find the similarities between it and Activity

This part is exactly the same as the Activity and instead of memorizing it, let’s look at a different place

In fact, this is the part where people tend to confuse and forget the most. Let’s analyze:

Fragments have several more lifecycle callback methods than activities

  • OnAttach (Activity) Is called when a Fragment is associated with an Activity

  • OnCreateView (LayoutInflater, ViewGroup, Bundle) creates the F

  • OnActivityCreated (Bundle) Called when the Activity’s onCreated method returns

  • OnDestroyView () corresponds to the onCreateView method and is called when the Fragment’s view has been removed

  • OnDetach () is the opposite of the onAttach method, which is called when the Fragment is unassociated with the Activity. Note: All methods except onCreateView must be called if you override the parent implementation of this method

    It’s not hard to understand but it takes some work to keep it in mind perfectly

    Let’s start with one by one:

    1. First, the onAttach method: this should be called first when associating with an Activity

    2. We know that the Activity needs to call setContentVIew() in the onCreate method to load the layout, so onCreateView in the Fragment is the equivalent of the Activity’s setContentVIew

    3. OnActivityCreate is an additional method to tell the Fragment how the current Activity is being created for subsequent actions of the Fragment

    order

    We know that fragments are dependent on activities and they all have the same lifecycle methods so which one calls the Activity first or the Fragment first? There are two cases

    • If so, create the Activity first and then create the Fragment

    • If destroy, destroy the Fragment first and then the Activity

      There are many articles on the Internet saying that the onCreate method of the Activity is executed after the onCreateView of the Fragment. This is not correct. The Fragment is usually created in the onCreate() of the Activity or by loading the layout You can either create a Fragment object by creating a new Fragment object if you don’t have an onCreate Activity you can’t have a Fragment

      conclusion

      After the above understanding, let’s rearrange the memory

      Activity is ActivityCreated and Fragment is ActivityCreated

      I hope it helped you