Android Fragment Lifecycle

举报
yd_221104950 发表于 2021/04/09 00:08:47 2021/04/09
【摘要】 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. ...

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 definitions
  • FragmentManager: The class for interacting with fragment objects inside an activity
  • FragmentTransaction: 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) }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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)

  
 
  • 1

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()

  
 
  • 1
  • 2
  • 3
  • 4

Demo

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/115461419

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。