Android fragment Detailed (V): Fragment and Activity communication

Source: Internet
Author: User

Communication with activity

Although the fragment implementation is independent of activity, it can be used for multiple activity, but each activity contains a different instance of the same fragment.

Fragment can call the Getactivity () method to get the object of its activity easily, and then it can find the controls in the activity (Findviewbyid ()). For example:

Viewlistview =getactivity (). Findviewbyid (r.id.list); Similarly, the activity can be fragmentmanager to find the frament that it contains. For example:

    1. Examplefragment fragment = (examplefragment) Getfragmentmanager (). Findfragmentbyid (r.id.example_fragment

Activity responds to fragment events

Sometimes, you may need to share events with the activity fragment. A good idea is to define a callback interface in fragment and then implement it in the activity.

For example, it is also an example of the news program, which has a activity,activity containing two fragment. Fragmenta Displays news headlines, FRAGMENTB displays the contents of the title. Fragmenta must tell the activity when the user chooses a title, and then the activity tells FRAGMENTB,FRAGMENTB to show the corresponding content (why is it so troublesome?). Direct Fragmenta tell Fragmentb not on the line? Yes, but your fragment reduces the ability to reuse. Now all I have to do is tell the host about my event, and it's up to the host to decide what to do, so is reusability better? )。 In the following example, the Onarticleselectedlistener interface is defined in Fragmenta:

    1. Public static class Fragmenta extends listfragment{
    2. ...
    3. //container Activity must implement this interface
    4. public interface onarticleselectedlistener{
    5. public void onarticleselected (Uri articleuri);
    6. }
    7. ...

The activity then implements the interface Onarticleselectedlistener, notifying Fragmentb in Method Onarticleselected (). When fragment is added to the activity, the Fragment Method Onattach () is called, which is suitable for checking that the activity implements the Onarticleselectedlistener interface. The check method is to type-Convert the instance of the incoming activity as follows:

  1. Public static class Fragmenta extends listfragment{
  2. Onarticleselectedlistener Mlistener;
  3. ...
  4. @Override
  5. public void Onattach (activity activity) {
  6. Super.onattach (activity);
  7. try{
  8. Mlistener = (onarticleselectedlistener) activity;
  9. }catch (ClassCastException e) {
  10. throw New ClassCastException (activity.tostring () +"must implement Onarticleselectedlistener");
  11. }
  12. }
  13. ...

If the activity does not implement that interface, fragment throws a ClassCastException exception. If successful, the Mlistener member variable holds the instance of Onarticleselectedlistener. Fragmenta can then invoke the Mlistener method to share the event with the activity. For example, if Fragmenta is a listfragment, each time you select an item in the list, the Onlistitemclick () method of Fragmenta is called, and onarticleselected () is called in this method. To share events with the activity, as follows:

  1. Public static class Fragmenta extends listfragment{
  2. Onarticleselectedlistener Mlistener;
  3. ...
  4. @Override
  5. public void Onlistitemclick (ListView l,view v,int position,long id) {
  6. //append The clicked item ' s row ID with the content provider Uri
  7. Uri Noteuri =contenturis.withappendedid (articlecolumns.content_uri,id);
  8. //send the event and Uri to the host activity
  9. Mlistener.onarticleselected (Noteuri);
  10. }
  11. ...

Onlistitemclick () The parameter ID passed in is the selected row ID of the list, and the other fragment uses this ID to get the contents of the title from the ContentProvider of the program.

Copy to Google TranslateTranslation Results

Android fragment Detailed (V): Fragment and Activity communication

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.