update fragment ui based off of Activity's Broadcast Receiver

我怕爱的太早我们不能终老 提交于 2020-01-16 01:18:32

问题


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.



来源:https://stackoverflow.com/questions/14835523/update-fragment-ui-based-off-of-activitys-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!