First let me say I have read through many of the questions related to fragments on SO. However, I can't seem to find a situation quite the same as mine.
I have myActivity that is using the PageAdapter, each page being a fragment. I also have a service that receives updates about the network connections etc. The service triggers the receiver in myActivity. myActivity needs to update FragmentPage1 but because I am using a pageAdapter and creating my fragments at run time I cannot 'findFragmentbyId' etc. I do not need to pass any data I just need to trigger the function inside of the FragmentPage1 class. Please see code snippet below.
public class myActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
FragmentManager fm = getSupportFragmentManager();
mPagerAdapter = new PagerAdapter(fm);
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(this);
// add tabs. Use ActionBar for 3.0 and above, otherwise use TabWidget
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.addTab(bar.newTab()
.setText(R.string.fragment_page_1)
.setTabListener(new ActionBarTabListener(mPager)));
bar.addTab(bar.newTab()
.setText(R.string.fragment_page_2)
.setTabListener(new ActionBarTabListener(mPager)));
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive (Context context, Intent intent){
if(intent.getAction().equals(InterfaceManager.BROADCAST_UPDATE_CONNECTION_STATS)) {
updateFragmentPage2();
} else if (intent.getAction().equals(InterfaceManager.BROADCAST_UPDATE_RULES)) {
UpdateFragmentPage1();
}
}
};
}
public class FragmentPage2 extends Fragment implements OnCheckedChangeListener, OnClickListener {
public void UpdateFragmentPage2() {
//update list view
}
}