Update ui fragment based on active broadcast receiver

First let me say that I read many questions related to snippets on SO. However, it seems that I can not find a situation completely similar to mine.

I have myActivity that uses a PageAdapter, with each page being a fragment. I also have a service that receives updates about network connections, etc. The service starts the receiver in myActivity. myActivity needs to be updated with FragmentPage1, but since I use pageAdapter and create my fragments at runtime, I cannot findFragmentbyId, etc. I do not need to pass any data that I just need to call for the function inside the FragmentPage1 class. The following is a snippet of code.

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
source share
2 answers

Based on your code, here is 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 get the abstract method updateFragmentContent (), then you just need to change the index of the tab and there is no special type

Please note: the above is a cleaner way to do this. With your existing code, you can still use two separate types and call two separate methods to update the corresponding fragments.

Hope this helps.

+1
source

Due to the complex relationship between BroadcastReceiver , Fragment and Activity , I ran into a similar problem and decided to slip out of this rotation and used the following:

When the BroadcastReceiver onReceive() method receives a call, add boolean to SharedPreferences as an indication indicator that the Fragment should do something, and in the onResume() fragment method, execute the necessary logic based on the SharedPreferences boolean set in the BroadcastReceiver onReceive() method.

Remember that there are more effective methods, and that I have not tested this approach in a long-term application.

+1
source

Source: https://habr.com/ru/post/917865/


All Articles