continues today to explain the characteristics of Fragment components, mainly the interaction with the Activity and lifecycle relationship, we have already said Fragment is dependent on the Activity, and life cycle is bound together with the Activity. Here we look at the relationship between Fragment with Activity.
1, create an event for the Activity callback method
In some cases, you may need a fragment with activity sharing events. A good method is to fragment defines a callback interface, and requires the host activity to achieve it. When the activity receives a callback through the interface, if necessary, it can be in the layout of the other fragment share information. For example, if a new application in the activity has two fragment - one to display the list of articles (framgent A), another display content of the article (fragment B) - then framgent A must tell activity when a list item is selected, Then it can tell fragmentB to show article.
In this example, OnArticleSelectedListener interfaces declared in the fragment A:
public static class FragmentA extends ListFragment
{
//...
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(Uri articleUri);
}
//...
}
then fragment host activity is implemented OnArticleSelectedListener interface and override onArticleSelected () to notify the fragment B, from the fragment A upcoming event. To ensure that the host activity to implement this interface, fragment A of onAttach () callback method (when adding fragment to activity by the system call) as an argument by onAttach () to do the Activity type conversion to instantiate a OnArticleSelectedListener instance.
public static class FragmentA extends ListFragment
{
OnArticleSelectedListener mListener;
//...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implementOnArticleSelectedListener");
}
}
//...
}
if the activity does not implement the interface, fragment will throw a ClassCastException exception. Under normal circumstances, mListener members are to maintain a reference to the implementation of the activity OnArticleSelectedListener therefore fragment A can call the methods defined in the interface OnArticleSelectedListener share event for the activity. For example, if the fragment A is a subclass of ListFragment, each time the user clicks on a list item, the system calls the fragment in onListItemClick (), which then calls onArticleSelected () to assign the event to the activity.
public static class FragmentA extends ListFragment
{
OnArticleSelectedListener mListener;
//...
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Append the clicked item's row ID with the content provider Uri
Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
// Send the event and Uri to the host activity
mListener.onArticleSelected(noteUri);
//...
}
passed to onListItemClick () The id parameter is the item that was clicked row ID, activity (or fragment) is used to get from the application of ContentProvider article.
2, add items to the ActionBar
your fragment can implement onCreateOptionMenu () provides a menu item to the activity of the Options menu (so, Action Bar is the same). In order to receive calls this method, however, you must onCreate () is called during setHasOptionsMenu () to indicate that the fragment willing to add the item to the options menu (Otherwise, fragment will not receive the right onCreateOptionsMenu () call).
subsequently added to the Option menu from a fragment of any of the items, will be appended to the existing menu items back. When a menu item is selected, fragment also receives a onOptionsItemSelected () callback. It can be in your fragment layout by calling registerForContextMenu () to register a view to provide a context menu. When the user opens the context menu, fragment receives one pairs onCreateContextMenu () call when the user selects an item, fragment receives one pairs onContextItemSelected () call.
Note: Although the fragment you will receive it adds each menu item is selected, a callback, but the actual taken when the user selects a menu item, activity will first receive the corresponding callback. If the activity is on-item-selected callback implementation does not deal with the selected item, then the event will be passed to the fragment of the callback.
This rule applies to the Options menu and context menu.
3, handle fragment lifecycle
fragment lifecycle management, the majority of local and lifecycle management activity like. and activity, like, fragment can be in three states: < br /> Resumed
in the running activity in the fragment are visible.
Paused
Another activity is in the foreground and has focus, but this fragment is located The activity is still visible (foreground activity partially transparent or does not cover the entire screen).
Stopped
either host activity has been stopped, either fragment from the activity been removed but was added to the background stack.
stopped state fragment is still alive (all state and member information maintained by the system). However, its users are no longer visible, and if the activity is killed, he will be killed.
its corresponding diagram is as follows:
and activity, as you can use the Bundle maintained fragment of the state, if activity is to kill the process, and when the activity is re-created when You need to restore the fragment can be used when the state you can fragment the onSaveInstanceState () during the save state and can onCreate (), onCreateView () or onActivityCreated () during restore it.
lifecycle aspects of activity and fragment among the most important difference is how each stack in its background Save. By default, activity is stopped, it will be managed by the system into a preservation activity for background stack. (So users can use the BACK button to navigate to fall back to it).
However, only when you remove the fragment during a transaction when an explicit call addToBackStack () request Save instance, was placed in an activity managed by the host background stack.
addition, management fragment of the life cycle and life cycle management activity is very similar. Thus, "managing the activitylifecycle" the same practice also apply to fragment. You need to understand is, activity how it affects the lives of fragment's life.
4, with activity lifecycle coordination
fragment survival activity of the life cycle of a direct impact on the life cycle of fragment, every activity lifecycle callback behavior will cause each fragment similar callback.
For example, when the activity receives onPause () when, activity in each fragment will receive onPause ().
Fragment have some extra lifecycle callback methods, those are only dealing with the interaction of activity, in order to perform tasks such as creating and destroying fragment's UI action. These additional callback methods are:
- onAttach ()
When the fragment is bound to the activity is called (Activity will be passed) - onCreateView ()
create and fragment associated view hierarchy is called - onActivityCreated ()
When the activity of the onCreate () method is called to return - onDestroyView ()
When and fragment associated view hierarchy being removed called - onDetach ()
When fragment disassociated from the activity is called
fragment lifecycle process, and the host of its activity, shown in Figure 3. In this figure, you can see the activity in order to determine how each state fragment might receive callback method. For example, when the activity receives its onCreate (), activity of the fragment is received at most onActivityCreated ().
arrived resumed once the activity status, you can freely add and remove the activity fragment. Therefore, only when the activity is resumed state, fragment of the life cycle can be varied independently.
Anyway, when activity resumed leaving the state, fragment activity was again pushed into its own life cycle processes.
5, Summary
Fragment relevant knowledge temporarily will stop here, you can look directly APIDEMO Demo examples inside the program, if you do not know where the API Demo, please Baidu! Society needs its own answers to learn programming.
Related Articles:
Android-based - Use Fragment adapt to different screen and resolution
Android-based - Fragment control switching multiple pages
Reference:
http://developer.android .com / guide / components / fragments.html
Edited by mythou
Original blog, reproduced, please indicate the source: http://www.cnblogs.com/mythou/p/3224108.html
没有评论:
发表评论