Android fragment-callback for event creation for activity

Source: Internet
Author: User

In some cases, fragment and activity sharing events may be required. Defining a callback interface within fragment is a good method and requires that the callback method be implemented by the activity holding it. When an activity receives a callback through an interface, it can share information with other fragment in the layout if necessary.

For example, if a news application has two fragment items in an activity, one for displaying the article list (fragment a) and the other for displaying the article content (fragment B) --- when the list item is selected, fragment A must tell activity so that it can tell fragment B to display the corresponding article. In the following example, the onarticleselectedlistener interface is declared in fragment.

Public static class fragmenta extends listfragment {
...
// Container activity must implement this interface
Public interface onarticleselectedlistener {
Public void onarticleselected (URI articleuri );
}
...
}

Then, the activity holding this fragment must implement the onarticleselectedlistener interface, and rewrite the onarticleselected () method to notify fragment B of the event from fragment. Make sure that the activity holding fragment implements this interface. The onattach () callback method of fragment A (the system calls this method when fragment is added to the activity) is converted to onattach () by the type conversion method () the input activity is used to instantiate an onarticleselectedlistener instance.

Public static class fragmenta extends listfragment {
Onarticleselectedlistener mlistener;
...
@ Override
Public void onattach (activity ){
Super. onattach (activity );
Try {
Mlistener = (onarticleselectedlistener) activity;
} Catch (classcastexception e ){
Throw new classcastexception (activity. tostring () + "must implement onarticleselectedlistener ");
}
}
...
}

If this activity does not implement this interface, fragment will throw a classcastexception. If successful, the mlistener member will have a reference to the onarticleselectedlistener object implemented by the activity, so that fragment A can define the callback method and activity sharing event through the onarticleselectedlistener interface. For example, if listfragment inherits fragment A, the system calls the onlistitemclick () method in fragment each time you click a list item, and then calls the onarticleselected () method and activity sharing event:

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 );
}
...
}

The ID parameter passed to onlistitemclick () is the row ID of the project to be clicked. Activity (or other fragment) uses this ID to obtain the corresponding article from the contentprovider object of the application.

For more information about using the valid content providers, see the content providers documentation.

 

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.