Android Api Component --- translation Fragment Component (2), androidfragment

Source: Internet
Author: User

Android Api Component --- translation Fragment Component (2), androidfragment

Let's continue with the previous article. Android Api Component --- Fragment Component translation (1)


Communication with activity

Although a Fragment is implemented independently of an Activity as an object and used in multiple activities, the given fragment instance is bound to the activity containing it.


Specifically, this fragment uses getActivity () to access the activity instance and easily execute tasks like searching for a view in the activity layout:

View listView = getActivity().findViewById(R.id.list);


Similarly, by requesting a Fring from FragmentManager to Fragment, your activity can call methods in fragment and use findFragmentById () or findFragmentByTag (). For example:

ExampleFragment fragment = (ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment);


Create Event Callback to activity

In some examples, you may need a fragment to share the event with the activity. A good way is to define a callback interface in fragment and require the main activity to implement it. When an activity receives a callback through this interface, it can share information with other fragment when necessary.


For example, if A news application has two fragment items in the activity-one is to display the list of articles (fragment A), and the other is to display an article (fragment B ), when a list item is selected, this fragment must tell this activity to explicitly tell fragment B this article. In this example, fragment A defines the OnArticaleSelectedListener interface:

public static class FragmentA extends Fragment {    ......    //Container Activity must implement this interface    public Interface OnArticleSelectedListener {        public void onArticleSelected(Uri articleUri);    }}


Then, the main activity of fragment implements the OnArticleSelectedListener interface and overwrites onArticleSelected () to notify fragment B of events from fragment. To ensure that the main activity implements this interface, the onAttach () of fragment A (the system will call this method when framgment is added to the activity) callback method maps the Activity to onAttach () to initialize an OnArticleSelectedListener:

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 implement OnArticleSelectedListener");        }    }}

If this activity has not implemented this interface, fragment will throw a ClassCastException. The key to success is that the mListener member holds an OnArticleSelectedListener implementation mapped to the activity, so that fragment A can share the event with the activity by calling methods defined in the OnArticleSelectedListener interface. For example, if fragment A is an extension of ListFragment, the system calls onListItemClick () in fragment every time you click A list item. This method can call onArticleSelected () share the event with this activity:

public static class FragmentA extends ListFragment {    OnArticleSelectedListener mListener;    ...        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);    }    ....}


The id parameter passed to onItemClick () is the row ID of the item to be clicked. This ID is used for activity (or other fragment) to capture articles from the ContentProvider of the application.

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.