Brief discussion on OnCreate () and Oncreateview ()

Source: Internet
Author: User

Oninflate () and Onattach ()

Fragment can have a view hierarchy to interact with the user. This view level can be created based on the XML layout file (inflated) or through code. The view level needs to be associated with the activity to be seen by the user. The initial two life cycles of fragment are oninflate and Onattach.

/* Left fragment is list, like Listactivity, inherits Listfragment */
public class Titlefragment extendslistfragment{
Private fragmentbasictest myactivity = null;
private int mcurcheckposition = 0;

...... Slightly......

@Override//"2" calls Onattach () after fragment is associated with activity. Because the specific implementation of the right fragment is implemented in the activity's ShowDetails (), you need to get the object of the activity. Of course, it can be obtained directly with getactivity ().
public void Onattach (activity activity) {
Showinfo ("Onattach () is called. Activity is "+ activity";
Showinfo ("Getactivity ():" + getactivity ());

Super.onattach (activity);
MyActivity = (fragmentbasictest) activity;
}

@Override"1" is called first: when the activity calls Setcontentview (), there is a <fragment> in the XML. The bundle stores the state of the fragment, not the initialized value. In general, it is too early to be associated with activity, and it is seldom necessary to deal with it here unless we need to deal with fragment properties or parameters. In the API's reference, fragment's main lifecycle is not even included in the Oninflate (), so it is less used in actual programming.
public void oninflate (activity activity, AttributeSet attrs, Bundle savedinstancestate) {
Showinfo ("Oninfalte () is called");
Showinfo ("activity:" + activity);
Showinfo ("Getactivity ():" + getactivity ());Fragment is still associated activity
showinfo ("attribute is the zodiac of fragment, for example r.id.titles =" + r.id.titles);
for (int i = 0; i< attrs.getattributecount (); i + +) {
showinfo ("attribute" + i + ":" + attrs.getattributename (i) + "=" + Attrs.getattributevalue (i));
}
Showinfo ("bundle:" + savedinstancestate);
super.oninflate (activity, attrs, savedinstancestate);
}

private void Showinfo (String s) {
LOG.D ("Titlefragment", s);
}
}

Let's look at the running Logcat display. As we can see, when the activity calls Setcontentview, the Onflate () in Flagment is called First, and then Onattach () is called. In Oninfalte () you can read the properties that we set in the XML file <fragment ... >. In the subsequent implementation of the fragment to the right, because the layout file in <fragment ...> does not specify the class attribute, but is implemented in the code, so there is no oninflate () state.

association with Activity : See Oninfalte () and Onattach () can get activity object, but if take getactivity () to obtain, then find Oninfalte () stage to process UI too early, cannot be linked to activity.

parameters : Throughout the lifecycle, parameters can be obtained through getarguments (), but cannot be setargments () after the activity is associated, only at very early stages of initialization, such as constructors and Oninflate ().

OnCreate () and Oncreateview ()

@Override   //"3" is called when it is initialized in the creation fragment. Note that the bundle parameter here is the state of the storage fragment. If we are going to start a background thread for fragment, consider putting the code here.  
public void onCreate (Bundle savedinstancestate) {  
     showinfo ("OnCreate () is called.");
     super.oncreate (savedinstancestate);


@Override  //"4" to create the fragment and return the fragment view hierarchy for the design of the UI. This example inherits the Listfragment implementation, which generates a ListView, whose resource ID is android.r.id.list1, or can be obtained through Getlistview (). We do not need to do special processing here, only to track the life cycle.
public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { 
     showinfo ("Oncreateview () is called.");
     return Super.oncreateview (Inflater, container, savedinstancestate);

Both OnCreate () and Oncreateview () are called when the activity calls Setcontentview (). That is, the activity does not complete the oncreate (), so you should not join the activity's view-related code here. The later life cycle is called after the activity completes OnCreate ().

Onactivitycreated ()

Onactivitycreated, as the name implies, is called after the activity completes OnCreate (). We can handle the other UI actions of the activity here, while the other fragment are already associated with the activity and we can deal with each other. This is also the last state before the user sees the UI interface, which is important for recreating activity and fragment from the saved state.

Recall that in Listactivity, the adapter is set up behind Setcontentview () because Setcontentview has provided the view level, Therefore, the specific settings list in fragment is also placed in the life cycle onactivitycreated () after fragment's Oncreateview ().

@Override//"5" onactivitycreated () executes after activity completes OnCreate (), after which UI design (Oncreateview ()) can be added, the UI is seen by the user before the code.
public void onactivitycreated (Bundle savedinstancestate) {
Showinfo ("onactivitycreated () is called.");
super.onactivitycreated (savedinstancestate);
//1) connection of view and data via adapter
Setlistadapter
(New Arrayadapter<string> (getactivity),//Note: in ListfragmentNoTo be associated directly with the ListView (via ID or Getlistview ()), and with Setlistadapter ()
Android. R.layout.simple_list_item_1,
Booksinfo.titles));
ListView LV = Getlistview ();
Lv.setchoicemode (Listview.choice_mode_single);
Lv.setselection (mcurcheckposition);
//2) Sets the right framgent UI according to the title.
Myactivity.showdetails (mcurcheckposition);
}

OnStart (), Onresume (), OnPause () and OnStop ()

These states are similar to the corresponding bodies of activty, which correspond to visual, interactive, non-interactive and non-visual. In contrast to the activity's life cycle diagram, the process of returning from an invisible background to the foreground is OnStop ()->onrestart ()->onstart (). and fragment is OnStop ()->onstart ().

Ondestroyview (), OnDestroy and Ondetach ()

When fragment allows a view-related resource to be released, call Ondestroyview (), emptying the view returned by Oncreateview (). Next, OnDestroy () clears the final fragment state. Finally, call Ondetach () to delete the association with the activity ().

Interacting with users

@Override //When the user clicks on the title, the relevant content is displayed in the right fragment. The listfragment can be implemented directly through the callback function Onlistitemclick.
public void Onlistitemclick (ListView l, View v, int position, long ID) {
Mcurcheckposition = position;
Myactivity.showdetails (mcurcheckposition);
}

This post covers the example code that can be downloaded in Pro Android Learning: Fragment.

(EXT) onCreate () and Oncreateview () talking about

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.