Android class reference --- Fragment (6)

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201206/134435.html


Public void onInflate (Activity activity, AttributeSet attrs, Bundle savedInstanceState)

This method is called when a Fragment object is filled as part of a View object layout and is usually used to set the content window of an Activity. After creating the Fragment object from the label of the layout file, you can call this object immediately. Note: The call is before the onAttach (Activity) method of the Fragment object is called. Therefore, all you can do at this time is to parse and save its property settings.

Each time this method is called, the Fragment object is filled, even if it is filled into a new instance for saving status. Parameters must be re-parsed every time, so that they can be changed according to different configurations.

This method is introduced in API Level 12.

The following is a typical Implementation of Fragment. It can obtain parameters through both the property and getArguments () methods:

PublicstaticclassMyFragmentextendsFragment {
CharSequence mLabel;

/**
* Create a new instance of MyFragment that will be initialized
* With the given arguments.
*/
StaticMyFragment newInstance (CharSequence label ){
MyFragment f = newMyFragment ();
Bundle B = newBundle ();
B. putCharSequence ("label", label );
F. setArguments (B );
Return f;
}

/**
* Parse attributes during inflation from a view hierarchy into
* Arguments we handle.
*/
@ Overridepublicvoid onInflate (Activity activity, AttributeSet attrs,
Bundle savedInstanceState ){
Super. onInflate (activity, attrs, savedInstanceState );

TypedArray a = activity. obtainStyledAttributes (attrs,
R. styleable. FragmentArguments );
MLabel = a. getText (R. styleable. FragmentArguments_android_label );
A. recycle ();
}

/**
* During creation, if arguments have been supplied to the fragment
* Then parse those out.
*/
@ Overridepublicvoid onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

Bundle args = getArguments ();
If (args! = Null ){
MLabel = args. getCharSequence ("label", mLabel );
}
}

/**
* Create the view for this fragment, using the arguments given to it.
*/
@ OverridepublicView onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
View v = inflater. inflate (R. layout. hello_world, container, false );
View TV = v. findViewById (R. id. text );
(TextView) TV). setText (mLabel! = Null? MLabel: "(no label )");
TV. setBackgroundDrawable (getResources (). getDrawable (android. R. drawable. gallery_thumb ));
Return v;
}
}

Note that the styleable resource is used to parse XML attributes. The XML Declaration used by styleable is as follows:

<Declare-styleablename = "FragmentArguments">
<Attrname = "android: label"/>
</Declare-styleable>

Then, within the content layout of the Activity, a Fragment tag can be declared as follows:

<Fragmentclass = "com. example. android. apis. app. FragmentArguments $ MyFragment"
Android: id = "@ + id/embedded"
Android: layout_width = "0px" android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: label = "@ string/fragment_arguments_embedded"/>

Fragment objects can also be dynamically created by parameters in Bundle objects at runtime. The following is an example of dynamic creation of Fragment objects:

@ Overrideprotectedvoid onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. fragment_arguments );

If (savedInstanceState = null ){
// First-time init; create fragment to embed in activity.
FragmentTransaction ft = getFragmentManager (). beginTransaction ();
Fragment newFragment = MyFragment. newInstance ("From Arguments ");
Ft. add (R. id. created, newFragment );
Ft. commit ();
}
}

Parameters

Activity specifies the Activity object to be filled with by this Fragment object;

Attrs specifies the property in the label of the Fragment object being created;

SavedInstanceState if the Fragment reconstructs the state to be retained, this parameter is used to save the state before the Fragment object.

Public void onLowMemory ()

This method is called when the entire system runs in a low memory state and the active running process view recycles the memory. The exact time point of this method is not defined. Generally, it occurs before and after all background processes are killed. This is before the time point of killing the process hosting service, and try to avoid killing the front-end UI.

The application can implement this method to release the cache or other unnecessary resources. After returning from this method, the system will perform the gc (garbage collection) operation.

Public boolean onOptionsItemSelected (MenuItem item)

This method is called when the option menu is selected. The default implementation of this method is simply to return false. The default implementation is to execute some normal processing (such as calling the Runnable object of the menu project or sending a message to the appropriate Handler object ). You can use this method of menu items to do the work that has no other measures.

Any subclass of this class should call the implementation of the base class to execute the default menu processing.

Parameters

The menu item selected by the user.

Return Value

Boolean value. If false is returned, normal menu processing continues. Otherwise, execution is terminated.

 

Public void onOptionsMenuClosed (Menu menu)

When the option menu is disabled (you can cancel the menu by pressing the "back" or the menu button, or select a menu item), the system will call this method.

Parameters

The option menu displayed at the end of the menu or initialized for the first time by the onCreateOptionsMenu () method.

Public void onPause ()

This method is called when the Fragment object is no longer in the recovery state. This method is usually bound with the Activity. onPause () method of its Activity lifecycle.

Public void onPrepareOptionsMenu (Menu menu)

This method is used to prepare the standard option menu for displaying the screen. It is called before the option menu is displayed. You can use this method to enable or disable some menu items, or modify the content of menu items dynamically.

Parameters

The option menu displayed at the end of the menu or initialized for the first time by the onCreateOptionsMenu () method.

Public void onResume ()

This method is called when the Fragment object is displayed to the user and is in an active running state. It is usually bound to the Activity. onResume () method of its Activity lifecycle.

Public void onSaveInstanceState (Bundle outstate)

When the Fragment object is required to save the current dynamic state, the system will call this method so that these saved states can be used when new instances are rebuilt in the future. If you need to create a new Fragment object instance in the future, the data placed in the Bundle parameter of this method will be passed to onCreate (Bundle), onCreateView (LayoutInflater, ViewGroup, Bundle) and the Bundle parameter of the onActivityCreated (Bundle) method ).

Most discussions about the corresponding Activity. onSaveInstanceState (Bundle) method also apply to this method. Note that this method can be called at any time before the onDestroy () method. In some cases, this Fragment object has been closed (for example, when it is placed in a rollback stack without the UI display), but until its Activity needs to save its status, the Fragment status is saved.

Parameters

Outstate: this parameter is used to place the State to be saved. It is a Bundle type object.

 

 

From FireOfStar's column

Related Article

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.