Android-fragment details

Source: Internet
Author: User

Fragment is introduced as part of the release of android3.0 (api level 11). Fragment allows the activity to be split into multiple completely independent encapsulated reusable components, each component has its own lifecycle and ui layout.

Fragment contains a series of Event Handlers similar to the acitivity class. When Fragment is created, started, restored, paused, stopped, and destroyed, these event handlers are triggered. Fragment also contains some additional callback used to identify the binding and unbinding relationship between Fragment and its parent acitivity, And the creation (and destruction) of the view hierarchy of Fragment, and the completion of the creation process of its parent activity. As shown in:

The program list shows a basic method available in the Fragment lifecycle. In the comment of each basic method, it describes the events for each state change. You should consider the actions to be taken into consideration.

Package com. example. todolist; import android. app. activity; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; public class MyFragment extends Fragment {// when this method is called, Fragment will be connected to its parent activity @ Overridepublic void onAttach (Activity activity) {// TODO Auto-generated method stubsuper. onAttach (activity); // gets a reference to the parent activity} // Call this method to perform initial fragment creation @ Overridepublic void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); // initialize fragment} // call this method @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, bundle savedInstanceState) {// TODO Auto-generated method stub // create, or fill in the fragment ui and return it // if this fragment does not have a ui, then return nullreturn su Per. onCreateView (inflater, container, savedInstanceState);} // call this method @ Overridepublic void onActivityCreated (Bundle savedInstanceState) once the ui of the parent activity and fragment has been created) {// TODO Auto-generated method stubsuper. onActivityCreated (savedInstanceState ); // complete fragment initialization-especially those tasks that can be done after the parent activity is initialized or the view of fragment is fully filled} // called at the beginning of the visible Lifecycle @ Overridepublic void onStart () {// TODO Auto-generated method stubsuper. onSta Rt (); // apply all required ui changes, now fragment is visible} // called at the beginning of the activity lifecycle @ Overridepublic void onResume () {// TODO Auto-generated method stubsuper. onResume (); // resume all ui updates, threads, or processes required for paused fragment, but in the inactive state, it is paused.} // call @ Overridepublic void onPause () {// TODO Auto-generated method stubsuper at the end of the activity lifecycle. onPause (); // when the activity is not the active front-end activity, you need to pause the ui update, suspend the thread, or pause the concentrated processing of the cpu that does not need to be updated. // After you call this method, the process may be terminated, so keep all edits and status changes.} // At the end of the activity lifecycle, call this method to keep the ui status changed @ Overridepublic void onSaveInstanceState (Bundle outState) {// TODO Auto-generated method stubsuper. onSaveInstanceState (outState); // Save the ui state change information to outstate. // This bundle will be passed to oncreate, oncreateview and onactivitycreate (if its parent activity is terminated and restarted) method} // call this method at the end of the visible life cycle @ Overridepublic void onStop () {// TODO Auto-generated method stubsuper. onStop (); // when fragment is invisible, pause other ui updates, suspend threads, or pause unnecessary processing} // when the fragment view is separated, call this method @ Overridepublic void onDestroyView () {// TODO Auto-generated method stub // clear viewsuper related to the resource. onDestroyView ();} // call this method at the end of the lifecycle @ Overridepublic void onDestroy () {// TODO Auto-generated method stub // clear all resources, including starting and closing database connections and other super. onDestroy ();} // when fragment is separated from its parent activity, this method @ Overridepublic void onDetach () {// TODO Auto-generated method stubsuper is used. onDetach ();}}
Demo Application cut:

The information in the interface is divided into two parts: fragment, EditText on the left for the user input, and ListFragment on the right for the user input information and input time. Meanwhile, ListItem is customized, and Adapter and TextView are modified.

Source code, click to download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.