微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android Fragment Lifecycle

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐