update fragment ui based off of Activity's Bro

2019-07-31 10:03发布

问题:

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
    }

}

回答1:

Based on your code, Here's what you can do quickly.

int tabIndex = 0;
MyCustomFragment frag = getFragmentManager().findFragmentByTag(getActionBar().getTabAt(tabIndex).getText().toString());
frag.updateFragmentContent();

Create a custom base fragment MyCustomFragment and have an abstract method updateFragmentContent(), then you'd just need to change the tab index and no special typecast

Please note, The above is a cleaner way to do it. With your existing code, you can still have two separate type cast and call two separate methods to update corresponding fragments.

Hope this helps.



回答2:

Due to the complex communication between the BroadcastReceiver, Fragment and Activity, I faced a similar problem and chose to slip out of that twist, and used the following:

When the BroadcastReceiver onReceive() method gets called add a boolean to the SharedPreferences as an indication flag that the Fragment should do something, and in the fragments onResume() method do the needed logic based on the SharedPreferences boolean set in the BroadcastReceiver onReceive() method.

Be informed though that there are better practices, and that I did not test such an approach on a long running term application.