How to Refresh Fragment from Action Bar Menu

Alex Aung

I have an Action Bar with Fragment as follow. I would like to refresh current fragment using Refresh button Action Bar Menu. I saw a lot of example using getFragmentByTag() but my fragment is created dynamically. May I know how to get the current fragment and refresh the content.

 
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

RssFragmentPagerAdapter mRssFragmentPagerAdapter; ViewPager mViewPager; List<RssCategory> categoryList; // Database Helper private DatabaseHelper db; private ActionBar actionBar; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try{ db = DatabaseHelper.getInstance(getApplicationContext()); int categoryCount = db.getCategoriesCount(); // Create the adapter that will return a fragment for each of the three primary sections // of the app. mRssFragmentPagerAdapter = new RssFragmentPagerAdapter(getSupportFragmentManager(), categoryCount); // Set up the action bar. actionBar = getActionBar(); // Specify that the Home/Up button should not be enabled, since there is no hierarchical // parent. actionBar.setHomeButtonEnabled(false); // Specify that we will be displaying tabs in the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); // Set up the ViewPager, attaching the adapter and setting up a listener for when the // user swipes between sections. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mRssFragmentPagerAdapter); mViewPager.setOffscreenPageLimit(categoryCount - 1); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { // When swiping between different app sections, select the corresponding tab. // We can also use ActionBar.Tab#select() to do this if we have a reference to the // Tab. actionBar.setSelectedNavigationItem(position); } }); initialiseActionBar(); }catch(Exception e){ Log.e(getClass().getName(), e.getMessage()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.finish(); return true; case R.id.action_refresh: //TO REFRESH CURRENT Fragment return true; default: return super.onOptionsItemSelected(item); } } private void initialiseActionBar() { if(categoryList == null) categoryList = db.getAllCategories(); // For each of the sections in the app, add a tab to the action bar. for (RssCategory category : categoryList) { // Create a tab with text corresponding to the page title defined by the adapter. // Also specify this Activity object, which implements the TabListener interface, as the // listener for when this tab is selected. actionBar.addTab( actionBar.newTab() .setText(category.getName()) .setTabListener(this)); } } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary * sections of the app. */ public static class RssFragmentPagerAdapter extends FragmentPagerAdapter { private int pageCount; public RssFragmentPagerAdapter(FragmentManager fm, int pageCount) { super(fm); this.pageCount = pageCount; } @Override public Fragment getItem(int i) { switch (i) { default: // The other sections of the app are dummy placeholders. Fragment fragment = new RssFragment(); Bundle args = new Bundle(); args.putInt(RssFragment.ARG_CATEGORY_ID, i + 1); fragment.setArguments(args); return fragment; } } @Override public int getCount() { return pageCount; } /*@Override public CharSequence getPageTitle(int position) { return "Section " + (position + 1); }*/ } /** * A dummy fragment representing a section of the app, but that simply displays dummy text. */ public static class RssFragment extends Fragment { public static final String ARG_CATEGORY_ID = "category_id"; View rootView; private List<RssItem> resultList; List<RssWebSite> websiteList; ArrayList<String> urlList; ProgressBar progressBar; @SuppressWarnings("unchecked") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { try{ rootView = inflater.inflate(R.layout.fragment_rss_items_list, container, false); resultList = new ArrayList<RssItem>(); progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar); Bundle args = getArguments(); if(args != null){ DatabaseHelper db = DatabaseHelper.getInstance(rootView.getContext()); websiteList = db.getAllRssWebSiteByCategory(args.getInt(ARG_CATEGORY_ID)); urlList = new ArrayList<String>(); if(websiteList != null && websiteList.size() > 0){ for (RssWebSite website : websiteList) { urlList.add(website.getRssUrl()); } if(urlList.size() > 0) { GetRSSDataTask task = new GetRSSDataTask(); task.execute(urlList); } } } }catch(Exception e){ Log.e(getClass().getName(), e.getMessage()); } return rootView; } /** * This class downloads and parses RSS Channel feed. * * @author clippertech * */ private class GetRSSDataTask extends AsyncTask<ArrayList<String>, Void, List<RssItem> > { @Override protected List<RssItem> doInBackground(ArrayList<String>... urls) { try { for(String url : urls[0]) { // Create RSS reader RssReader rssReader = new RssReader(url); Log.d(getClass().getName(), url); // Parse RSS, get items resultList.addAll(rssReader.getItems()); } return resultList; }catch (Exception e) { Log.e(getClass().getName(), e.getMessage()); } return null; } @Override protected void onPostExecute(List<RssItem> result) { try{ // Get a ListView from the RSS Channel view ListView itcItems = (ListView) rootView.findViewById(R.id.rssChannelListView); View emptyView = null; if(result == null){ itcItems.setEmptyView(emptyView); Log.d(getClass().getName(), "Empty View"); } else { //resultList.addAll(result); Collections.sort(result, new Comparator<RssItem>() { @Override public int compare(RssItem lhs, RssItem rhs) { SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz"); try { Date date1 = formatter.parse(rhs.getPublishedDate()); Date date2 = formatter.parse(lhs.getPublishedDate()); return date1.compareTo(date2); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }); // Create a list adapter ListAdapter adapter = new ListAdapter(rootView.getContext(), resultList); itcItems.setAdapter(adapter); adapter.notifyDataSetChanged(); // Set list view item click listener itcItems.setOnItemClickListener(new ListListener(resultList, getActivity())); } //dialog.dismiss(); progressBar.setVisibility(View.GONE); }catch(Exception e){ Log.e(getClass().getName(), e.getMessage()); } } } }

}

Kuffs

You would need to keep track of the current fragment index by using the PageChangeListener in your viewpager.

You can use the fragment index to retrieve the fragment from your adapter and call whatever methods you need on it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Refresh Fragment from Action Bar Menu

From Dev

Android How to edit the Action bar menu from Fragment

From Dev

android fragment action bar menu

From Dev

How To Start A Fragment From Spinner in action bar?

From Dev

How to change menu items on action bar when fragment changes

From Dev

Action bar Icon menu issue on Fragment

From Dev

Action bar menu for each tab fragment with ActionBarSherlock

From Dev

Change fragment layout with action bar menu

From Dev

Action bar menu is not shown in Fragment Activity

From Dev

How to display fragment in container when selected from an action bar?

From Dev

How do I open a drop menu from a button in action bar

From Dev

How to remove an item from the action bar drop down navigation menu

From Dev

How do I open a drop menu from a button in action bar

From Dev

Filter fragment adapter from action bar

From Dev

How to refresh fragment B from fragment A?

From Dev

How to show action bar icons's on a fragment?

From Dev

How to hide Action bar in splash screen fragment

From Dev

How to show viewpager fragment position on action bar?

From Dev

how can I disable the options menu from action bar(not the menu items)

From Dev

How to change option menu icon in the action bar?

From Dev

How to add menu button without action bar?

From Dev

Android - How to hide the Menu Button in Action Bar

From Dev

Open a float context menu from an action bar menu option

From Dev

Toolbar as action bar in Fragment

From Dev

How to refresh a fragment from an activity in android?

From Dev

How to refresh Fragment from different java file?

From Dev

Add progress dialog in to the action bar sherlock from sherlock fragment

From Dev

Android: Calling a function inside a fragment from a custom action bar

From Dev

Go back Through Action Bar from Fragment To Activity

Related Related

  1. 1

    How to Refresh Fragment from Action Bar Menu

  2. 2

    Android How to edit the Action bar menu from Fragment

  3. 3

    android fragment action bar menu

  4. 4

    How To Start A Fragment From Spinner in action bar?

  5. 5

    How to change menu items on action bar when fragment changes

  6. 6

    Action bar Icon menu issue on Fragment

  7. 7

    Action bar menu for each tab fragment with ActionBarSherlock

  8. 8

    Change fragment layout with action bar menu

  9. 9

    Action bar menu is not shown in Fragment Activity

  10. 10

    How to display fragment in container when selected from an action bar?

  11. 11

    How do I open a drop menu from a button in action bar

  12. 12

    How to remove an item from the action bar drop down navigation menu

  13. 13

    How do I open a drop menu from a button in action bar

  14. 14

    Filter fragment adapter from action bar

  15. 15

    How to refresh fragment B from fragment A?

  16. 16

    How to show action bar icons's on a fragment?

  17. 17

    How to hide Action bar in splash screen fragment

  18. 18

    How to show viewpager fragment position on action bar?

  19. 19

    how can I disable the options menu from action bar(not the menu items)

  20. 20

    How to change option menu icon in the action bar?

  21. 21

    How to add menu button without action bar?

  22. 22

    Android - How to hide the Menu Button in Action Bar

  23. 23

    Open a float context menu from an action bar menu option

  24. 24

    Toolbar as action bar in Fragment

  25. 25

    How to refresh a fragment from an activity in android?

  26. 26

    How to refresh Fragment from different java file?

  27. 27

    Add progress dialog in to the action bar sherlock from sherlock fragment

  28. 28

    Android: Calling a function inside a fragment from a custom action bar

  29. 29

    Go back Through Action Bar from Fragment To Activity

HotTag

Archive