Thanks for all the help on this.  I'm not the greatest Java
programmer, but thought I would share my code in case it's useful to
anyone else.

In my case, I'm trying to replicate the functionality similar to the
email client.  I have 3 fragments - a list view, a gallery view, and a
content view.  At start up, I show only the list view and gallery
view.  When a user clicks on a title in the gallery view, I hide the
list view, slide the gallery view over to the left, display the
content view and tell the content view which title to show.  The flow
then is the gallery fragment tells the activity what title was
selected, the activity updates the UI and calls a method in the
content fragment.

Here is my code.  I tried to make it as generic as possible, but it
probably doesn't have enough error handling.

=== FragmentNotification.java ===

import android.app.Fragment;

//Allows a fragment to notify an activity of an event
public interface FragmentNotification
{
        public static final int  FRAG_ACTION_TITLE_SELECTED = 1;

        // actionInt and actionString are optional depending on actionType
        void fragmentAction(Fragment fragID, int actionType, int actionInt,
String actionString );
}

=== GalleryFragment.java ===

        gView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int
position, long id)
            {
                        rTitle = xmlparse.results.RecordList.get(position);

                        GalleryFragment galleryFrag = (GalleryFragment)
getFragmentManager()
                        .findFragmentById(R.id.frag_gallery);

                        ((FragmentNotification)getActivity()).fragmentAction
                                (galleryFrag, 
FragmentNotification.FRAG_ACTION_TITLE_SELECTED,
0, rTitle.sTitleId);

            }
        });

=== MainActivity.java ===

...
public class MainActivity extends Activity implements
FragmentNotification, ActionBar.TabListener,
SearchView.OnQueryTextListener, SearchView.OnCloseListener,
Button.OnClickListener,
{
...

        //Receive an event notification from a fragment
        @Override
        public void fragmentAction(Fragment fragID, int actionType, int
actionInt, String actionString)
        {
                if (fragID == fragGallery)
                {
                        if (actionType == 
FragmentNotification.FRAG_ACTION_TITLE_SELECTED)
                        {
                        //Update user interface
                        if (detailState != DETAIL_STATE_DETAIL)
                                setDetailState(DETAIL_STATE_DETAIL);

                        //Tell content frag to display title
                        fragContent.displayTitle(actionString);
                        }
                }

        }
...
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to