Bear
Bear

Reputation: 5152

Refresh the content of fragment

Here is an android programming question. I follow the offical example and write an activity that consist of two fragments which show a list menu and content. When user click a item in list, detail will be shown in content fragment. Now, I want to add a refresh button in Actionbar. When user click it, content in content fragment will be reloaded.

But I don't know how to get the reference of the running content fragment and call it to refresh. (Suppose all content fragments have already implemented a method called refresh())

public class FragmentLayout extends Activity {
   .....
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_reload:      //refresh button is clicked
                 //how can I call the DetailFragment to reload here?
            }
   public static class TitlesFragment extends ListFragment {
       ....
   }
   public static class DetailFragment extends Fragment {
       ...
   }    

In short, how the fragment and the activity contains the fragment to communicate?

Upvotes: 1

Views: 1199

Answers (1)

Jake Wharton
Jake Wharton

Reputation: 76115

Override onOptionsItemSelected in the fragment rather than the activity and react internally to the refresh event there.

Upvotes: 1

Related Questions