title: How to make an Android refresh button that refreshes the fragment in a drawer?
body: <p>I am trying to refresh the fragment depending on what drawer the user is on... </p>
  
  <p>This was my attempt which does not do anything but does not give me an error.</p>
  
  <p>Here is my code</p>
  
  <pre><code>public void fragmentRefresh() {
          // Insert the fragment by replacing any existing fragment
          FragmentManager fragmentManager = getFragmentManager();
  
              fragmentManager.beginTransaction()
                      .replace(R.id.frame_container, fragmentManager.findFragmentByTag("TagForRefresh"))
                      .commit();
  
  
      }
  </code></pre>
  
  <p>Here is the drawer fragment code:</p>
  
  <pre><code>/**
       * Diplaying fragment view for selected nav drawer list item
       */
      private void displayView(int position) {
          // update the main content by replacing fragments
          Fragment fragment = null;
          switch (position) {
              case 0:
                  fragment = new HomeFragment();
                  break;
              case 1:
                  fragment = new BrowseFragment();
                  break;
              case 2:
                  fragment = new InviteFragment();
                  break;
              case 3:
                  fragment = new ProfileFragment();
                  break;
              case 4:
                  fragment = new SettingsFragment();
                  break;
              case 5:
                  callConfirmLogout();
                  break;
  
  
              default:
                  break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .replace(R.id.frame_container, fragment, "TagForRefresh").commit();
  
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
  </code></pre>
  
  <p>Implemented in a tab swiper fragment class:</p>
  
  <pre><code>public class ViewVotesActivity extends FragmentActivity implements ActionBar.TabListener{
  
  
      //Tab options
      private ViewPager viewPager;
      private TabSwipeAdapter mAdapter;
      private ActionBar actionBar;
  
      // Tab titles
      private String[] tabs = { "Created", "Up", "Down"};
      //for custom menu to add progress bar to refresh
      private MenuItem menuItem;
  
      //Search view action bar
      private SearchView search_action_bar;
  
      //getApplicationContext().getString(R.string.created),getApplicationContext().getString(R.string.upvote), getApplicationContext().getString(R.string.downvote)
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_view_votes);
  
  
          // Initializing tab
          viewPager = (ViewPager) findViewById(R.id.pager);
          actionBar = getActionBar();
          mAdapter = new TabSwipeAdapter(getSupportFragmentManager(), new UserCreatedFragment(), new UserUpVotesFragment(),new UserDownVotesFragment());
  
          viewPager.setAdapter(mAdapter);
          actionBar.setHomeButtonEnabled(true);
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          // Adding Tabs
          for (String tab_name : tabs) {
              actionBar.addTab(actionBar.newTab().setText(tab_name)
                      .setTabListener(this));
          }
  
          /**
           * on swiping the viewpager make respective tab selected
           * */
          /**
           * on swiping the viewpager make respective tab selected
           * */
          viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
              @Override
              public void onPageSelected(int position) {
                  // on changing the page
                  // make respected tab selected
                  actionBar.setSelectedNavigationItem(position);
              }
  
              @Override
              public void onPageScrolled(int arg0, float arg1, int arg2) {
              }
  
              @Override
              public void onPageScrollStateChanged(int arg0) {
              }
          });
  
  
      }
  
      @Override
      public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
          // on tab selected
          // show respected fragment view
          viewPager.setCurrentItem(tab.getPosition());
      }
  
      @Override
      public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  
      }
  
      @Override
      public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.home, menu);
          setupActions(menu);
          callSearchActionBar(menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar actions click
          switch (item.getItemId()) {
              case R.id.action_refresh:
                  menuItem = item;
                  menuItem.setActionView(R.layout.refresh_progress_bar);
                  //menuItem.expandActionView();
                  fragmentRefresh();
  
                  TestTask task = new TestTask();
                  task.execute("test");
                  return true;
              case R.id.action_discover:
                  addNewItem();
                  return true;
              case R.id.action_search:
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      public void addNewItem() {
          // user redirect to register followup
          Intent createItemIntent = new Intent(ViewVotesActivity.this, CreateItemActivity.class);
          // Closing all the Activities
          createItemIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
          // Add new Flag to start new Activity
          createItemIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
          // Staring Login Activity
          startActivity(createItemIntent);
      }
  
      public void fragmentRefresh() {
  //        // Insert the fragment by replacing any existing fragment
  //        FragmentManager fragmentManager = getFragmentManager();
  //
  //        fragmentManager.beginTransaction()
  //                .replace(R.id.frame_container, fragmentManager.findFragmentByTag("TagForRefresh"))
  //                .commit();
  
  
      }
  
  
      private class TestTask extends AsyncTask&lt;String, Void, String&gt; {
  
          @Override
          protected String doInBackground(String... params) {
              // Simulate something long running
              try {
                  Thread.sleep(2000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              return null;
          }
  
          @Override
          protected void onPostExecute(String result) {
              menuItem.collapseActionView();
              menuItem.setActionView(null);
          }
      };
  
      public  void setupActions(Menu menu) {
          MenuItem refreshItem = menu.findItem(R.id.action_refresh);
          MenuItem discoverItem = menu.findItem(R.id.action_discover);
  
          if (isAlwaysExpanded()) {
  
          } else {
              refreshItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                      | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
  
              discoverItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                      | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
          }
  
      }
  
      public void callSearchActionBar(Menu menu) {
          //find the menu item and set search view at the same time
          MenuItem searchItem = menu.findItem(R.id.action_search);
  
          search_action_bar
                  = (SearchView)  searchItem.getActionView();
  
          if (isAlwaysExpanded()) {
              search_action_bar.setIconifiedByDefault(false);
          } else {
              searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                      | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
          }
  
          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
          if (searchManager != null) {
              List&lt;SearchableInfo&gt; searchables = searchManager.getSearchablesInGlobalSearch();
  
              SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
              for (SearchableInfo inf : searchables) {
                  if (inf.getSuggestAuthority() != null
                          &amp;&amp; inf.getSuggestAuthority().startsWith("applications")) {
                      info = inf;
                  }
              }
              search_action_bar.setSearchableInfo(info);
          }
  
          search_action_bar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
              public boolean onQueryTextChange(String newText) {
                  Log.e("Query = ", newText);
                  return false;
              }
  
              public boolean onQueryTextSubmit(String query) {
                  Log.e("Submit", "Submited " + query);
                  // user redirect to register followup
                  Intent searchIntent = new Intent(ViewVotesActivity.this, SearchActivity.class);
  
                  searchIntent.putExtra("search", query);
  
                  // Closing all the Activities
                  searchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
                  // Add new Flag to start new Activity
                  searchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
                  // Staring Login Activity
                  startActivity(searchIntent);
                  return false;
              }
  
              public boolean onClose() {
                  Log.e("query close", "query closingggg");
                  return false;
              }
          });
      }
      protected boolean isAlwaysExpanded() {
          return false;
      }
  
  
  }
  </code></pre>