About Fragment

I think many interviewers have asked this question before, how do you deal with different screens and screens of different resolutions, because the same layout is completely different on different screens, one of the solutions is to use dp, dp is not a definite scale. It will make relative positions with different screens. It is a bit like a scale. For example, if a button is placed 2dp from the left, and now it is replaced by a device twice as large, then the same, put it in The ratio is the same. Of course, you can directly adjust the Activity, but it can only be used once. How to use it multiple times on different devices? This involves the fragmentation problem, which is Fragment.

        After Android 3.0, a new core class is Fragment. In fact, I have been asked this question more than once by the interviewer. It seems that this question is particularly important when interviewing for Android. Let's have a good time with Fragment.

[Reuse] The reuse (chong) function is an important reason why Fragment is reused (zhong). After all, what you don't want to write hard can only be used once.

[horizontal screen and vertical screen] The original layout is up and down. After the horizontal screen, you certainly hope that it can be reasonably left and right, but on a small screen, this situation is very "embarrassing". Although both come out, but It was very crowded, so I thought of paging, swiping left and right to see these two pages, the concept of the right fragment is very close to paging, in fact, as an Activity, it can also achieve such a function, but it is overkill. The most important thing is that when the user clicks the back key, they are really logged out. Fragment, after clicking this back, is still in this activity.

 

【Life cycle of fragments】



 Although the fragment has its own life cycle, it is still attached to the Activity in the end. The specific code implementation can show this. You can read my last blog, the story of the boss and the little brother.

When the fragment is created: onAttach()-->onCreate()-->onCreateView()-->onActivityCreated();

When the fragment enters the background mode: onPause()-->onStop();

When the fragment becomes visible: onStart()-->onResume();

When the fragment is destroyed: onPause()-->onStop()-->onDestroyView()-->onDestroy()-->onDetach();

When onDetach, the fragment will not be bound to the activity. Resources are also released

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395873&siteId=291194637