Android Fragment development documentation translation-1

Source: Internet
Author: User
Tags string tags

Due to my limited English skills, please understand at the beginning
This blog is original as long as it does not indicate "Conversion". Please indicate the link for the post.

Android Fragment development documentation translation-1
Android Fragment development documentation translation-2
This series does not translate the original article 100% nor post the original article 100%.

Fragment is also a new component of android3.0 (api level 11 ).
[Java]
Public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListener
Known direct subclasses include
DialogFragment, ListFragment, PreferenceFragment, WebViewFragment

Fragment is closely bound to the Activity in which it is located and cannot be used independently.
Although Fragment defines its own declaration cycle, it also depends on its activity
If activity is stopped, fragments in the activity will not be started. When activity is destroyed, all fragments will also destroyed
A Fragment is closely tied to the Activity it is in, and can not be used apart from one. though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it can be started; when the activity is destroyed, all fragments will be destroyed

All subclasses of Fragment must contain a public empty constructor.
When necessary, especially when the state is restored, the framework will often instantiate a fragment class, and you need to find this constructor to instantiate it.
If the empty constructor is unavailable, an exception occurs when the state is restored.
All subclasses of Fragment must include a public empty constructor. the framework will often re-instantiate a fragment class when needed, in particle during state restore, and needs to be able to find this constructor to instantiate it. if the empty constructor is not available, a runtime exception will occur in some cases during state restore


The following figure shows the fragment lifecycle.


If fragment has a part to be displayed on the page, you must override onCreateView () and return a View (root of layout of fragment)
If no display is required, ignore this function.
If ListFargment is inherited, you do not need to override onCreateView (). The ListFargment has been rewritten.
[Java]
Public static class ExampleFragment extends Fragment {
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
// Inflate the layout for this fragment
Return inflater. inflate (R. layout. example_fragment, container, false );
}
}

Add fragment to activity (two methods)
A. Declare in layout of activity that you can specify the layout attribute for fragment like View
[Xml]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "horizontal"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Fragment android: name = "com. example. news. ArticleListFragment"
Android: id = "@ + id/list"
Android: layout_weight = "1"
Android: layout_width = "0dp"
Android: layout_height = "match_parent"/>
<Fragment android: name = "com. example. news. ArticleReaderFragment"
Android: id = "@ + id/viewer"
Android: layout_weight = "2"
Android: layout_width = "0dp"
Android: layout_height = "match_parent"/>
</LinearLayout>
Android: name specifies the class used to instantiate fragment.


B. Add fragment to ViewGroup in the program
You can add fragment to the layout of your activity at any time when your activity is running. You only need to specify a ViewGroup to place fragment.
The following is a demo of fragment transaction processing.
[Java]
FragmentManager fragmentManager = getFragmentManager ()
FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction ();
ExampleFragment fragment = new ExampleFragment ();
FragmentTransaction. add (R. id. fragment_container, fragment );
FragmentTransaction. commit ();
Once FragmentTransaction is used to change the fragment, you must call commit to make it take effect.

Each fragment requires a unique identifier. There are three methods to specify a unique identifier.
1. android: id
2. android: tag
3. If neither of the above two is provided, the system uses the ID of its container view as the identifier.

Add a fragment without a UI
Use add (Fragment, String) in the activity to add a fragment without UI (a tag with a unique string is better than a view id. In this way, fragment can be added, but because it is not associated with the layout View in the activity, it will not receive the onCreateView () call, so this method is not required.
Providing a string tag for fragment is not limited to fragment without UI. You can provide a string tag for fragment with UI, but if fragment has no UI, the string tag is the only way to identify it. If you want to get this fragment from the activity later, you need to use findFragmentByTag ()

To add a fragment without a UI, add the fragment from the activity using add (Fragment, String) (supplying a unique string "tag" for the fragment, rather than a view ID ). this adds the fragment, but, because it's not associated with a view in the activity layout, it does not receive a call to onCreateView (). so you don't need to implement that method.

Supplying a string tag for the fragment isn't strictly for non-UI fragments-you can also supply string tags to fragments that do have a UI-but if the fragment does not have a UI, then the string tag is the only way to identify it. if you want to get the fragment from the activity later, you need to use findFragmentByTag ().


An example of fragment without UI (the following blog will show the api demos learning about fragment)
ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance. java

You can use FragmentManager to manage fragment.
You can use getFragmentManager () to obtain the FragmentManager in the activity.
You can use FragmentTransaction to execute a fragment transaction.
[Java]
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction ();
A transaction is a set of changes that you submit to the activity. You can use the api in FragmentTransaction to execute the transaction.
You can also save each transaction to a back stack managed by the activity, allowing the user to navigate to the fragment changes (similar to the activities in the back navigation)

Before calling commit (), you may want to call addToBackStack () to add transaction to a back stack of the fragment transaction set.
This back stack is managed by the activity and allows the user to return to the previous fragment status by pressing the return key.

One example demonstrates how to replace another fragment with one fragment and save the previous state in the back stack.
[Java]
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment ();
FragmentTransaction transaction = getFragmentManager (). beginTransaction ();
 
// Replace whatever is in the fragment_container view with this fragment,
// And add the transaction to the back stack
Transaction. replace (R. id. fragment_container, newFragment );
Transaction. addToBackStack (null );
 
// Commit the transaction
Transaction. commit ();
In this example, newFragment replaces the fragment currently identified by ID: R. id. fragment_container in the layout container.
When addToBackStack () is called, the "replace" transaction is saved to the back stack. Therefore, you can roll back the transaction and press the back key to bring back the previous fragment.


If you add multiple changes to the transaction (such as another add () or remove () and call addToBackStack (),
Then all changes applied before you call commit () are added to the back stack as a single transaction and the Back button will reverse them all together.
If you add multiple changes to a transaction (such as another add or remove) and call addToBackStack (),
Before you call commit (), all implemented changes are added to the back stack as a single transaction, and the Back key will roll back them together.

Let's talk about the sequence.
The order is not important,:
Commit () must be called at the end ()
If multiple fragments are added to the same iner, the order they are added determines the order they are displayed at the view level.

If you do not call addToBackStack () when executing a transaction to remove the fragment operation (). After the transaction is submitted, the fragment will be destroyed, and the user cannot roll back.
Conversely, if you call addToBackStack () when removing fragment, this fragment will stop and if you navigate back to it, it will resumed
TIPS: For each fragment transaction, you can use a transition animation by calling setTransition () before commit ().

Calling commit () does not execute the transaction immediately. On the contrary, once the UI thread of the activity has the ability to complete the transaction, FragmentTransaction will include the commit in the UI thread of the activity to run.

If necessary, you can call executePendingTransactions () from your UI thread to execute the submitted transaction immediately through commit. This is usually not necessary unless transaction is the dependency of other threads.

Warning: You can use commit () to submit a transaction to save the status only before the activity (when the user leaves the activity ).
If you try to commit after this time point, you will receive an exception. This is because if the activity needs to be restored, the state after commit may be lost. If you think this commit can be lost, you can use commitAllowingStateLoss ()

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.