Android Fragment Lifecycle

Android Fragment Lifecycle,第1张

概述Accordingtoofficialdoc,FragmentisusedtobuilddynamicUserInterfaces.IhaveheardthatsomeappsjusthaveanActivitywithmanyfragments.That’scrazy!Butthatispossible.Fragmentshouldbeusedwithinanactivity.Actually,Fragmentshave

According to official doc, Fragment is used to build dynamic User Interfaces. I have heard that some apps just have an Activity with many fragments. That’s crazy! But that is possible.

Fragment should be used within an activity.Actually, Fragments have their own vIEw. A Fragment is added to a VIEwGroup insIDe an activity.And the vIEw of the Fragment is displayed insIDe this Activity’s VIEwGroup.

What is happening when a Fragment is added to the Activity’s VIEwGroup?

First, The activity obtains a reference to the fragment.Then it gets a reference to its VIEwGroup insIDe which the Fragments’s vIEw will be rendered.Then the activity adds the Fragment.The Fragment starts to create its vIEw and return it to the Activity.Then the VIEw is inserted into the activity’s VIEwGroup.Then the Fragment is alive.

The methods of Fragment lifecycle are shown below:

onAttach() This method will be called first. It indicates that the Fragment has been attached to the activity.onCreateVIEw() This method will be called ,when it is time for the Fragment to draw its vIEw for the first time.If your Fragment is not going to provIDe any UI,then you can return null in this method.onVIEwCreated() This method is called after onCreateVIEw() . It is very useful. You may need to configure the vIEws ,such as with a RecyclerVIEw, and when to set up an adapter.onActivityCreated() This method will be called after onCreate() and onCreateVIEw(). it is to indicate that the activity’s onCreate() has completed. So if there is something that is needed to be initialized in Fragment that depends upon the activity’s onCreate() having completed its work, then you do it here.onStart() This method will be called once the Fragment gets visible.onPause() This method will be called as the user is leaving the Fragment. This is usually where you should commit any changes that should be persisted for the current user session.onStop() This method will be call when the Fragment is go to be stopped.onDestroyVIEw() This method is the counterpart to onCreateVIEw() where we setup the UI of the Fragment. If there are things that are needed to be cleaned up to the UI, you can put up logic here.onDestroy() It is not guaranteed to be called by the system. You should be aware of this.onDetach() This is method is the counterpart to onAttach().It will be called after onDestroy(). It is used to notify that the Fragment has been disassociated from its hosting activity.

AndroID Fragment classes

Fragment : The base class for all fragment deFinitionsFragmentManager: The class for interacting with fragment objects insIDe an activityFragmentTransaction: The class for performing an atomic set of fragment operations.

Let’s talk about the main methods of Fragment!

onCreateVIEw()

overrIDe fun onCreateVIEw(        inflater: LayoutInflater, container: VIEwGroup?,        savedInstanceState: Bundle?    ): VIEw? {        // Inflate the layout for this fragment        return inflater.inflate(R.layout.fragment_home, container, false)    }

The method gets three parameters:LayoutInflater , VIEwGroup, Bundle.
We always use LayoutInflater parameter to create VIEw instance based on layout file. As you can see, following snippet is to create a vIEw based on layout file:

inflater.inflate(R.layout.fragment_home, container, false)

inflate() inflates a new vIEw hIErarchy from the specifIEd xml resource.inflate() takes three parameters:

resource: ID for an XML layout resource to load. e.g:R.layout.fragment_home .root: It is a VIEwGroup into which the fragment’s VIEw is to be inserted .Optional vIEw to be the parent of the generated hIErarchy (if attachToRoot is true), or else simply an object that provIDes a set of LayoutParams values for root of the returned hIErarchy (if attachToRoot is false.)attachToRoot : It is a boolean value. Whether the inflated hIErarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root vIEw in the XML.and last one is boolean tells us Whether the inflated hIErarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root vIEw in the XML.
You can use fragment in layout xml file. you can also use the following snippet code in Activity to use fragment:
supportFragmentManager                .beginTransaction()                .add(R.ID.container_end,MenuFragment())                .commit()

Demo

总结

以上是内存溢出为你收集整理的Android Fragment Lifecycle全部内容,希望文章能够帮你解决Android Fragment Lifecycle所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1022110.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存