How to refresh fragment tab content on button click

73,140

To refresh the contents of a fragment, you could keep a reference to the fragment, and call a public (for example) refresh() method in that fragment. Example:

public class ArticleTabFragmentActivity extends FragmentActivity{
  private PopulareFragment populareFragment;
  private RecomandateFragment recomandateFragment;

  <code>

  private void bindView(){
    Button btn_refresh = (Button) findViewById(R.id.btn_refresh);

    btn_refresh.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                 String current_tab = mTabHost.getCurrentTabTag(); 
                 if(current_tab.contains("POPULARE")){
                   populareFragment.refresh();
                 } else if(current_tab.contains("RECOMANDATE")){
                   recomandateFragment.refresh();
                 } 
    });
  }



}

public class PopulareFragment extends Fragment{  

  public void refresh(){
    <refresh the contents of the fragment>
  }

}

<Same for other fragment>

Now when you create the tab fragment, like the PupulareFragment, use the pupulareFragment instance variable to store the fragment. So it is available in the refresh button onClick method.

This way you are not replacing/creating any new fragments when refreshing. So going back to tabActivity after going to de article details activity, would probably work fine aswell.

Share:
73,140
mr.kosso
Author by

mr.kosso

Updated on July 15, 2022

Comments

  • mr.kosso
    mr.kosso almost 2 years

    I'm new in Android and I'm implementing a small project in which, I have news from one Drupal website. This app is with 3 tabs and every tab have a list of articles with specific content. The list of articles is created by textviews and imageviews arranged in linearlayout. When I click on the title, the article is opening with details. Those articles are loaded from Drupal site throught HTTP POST and ViewJSON module. Tabs are created with FragmentActivity and TabHost. Everything's ok and works fine until here.

    My problem is that I want to make one refresh button, to be placed over tabs and when I press it, the list of articles must refresh or reload and keep the current tab opened. I tried to replace tab fragment content and re-open it, but when I click on the title of one article to open it, the tab content remain in stack under all fragments. I mention that when I click on article's title, one new fragment is opening. I sent an id of the articles as an argument and in the same fragment I open the article's content.

    I was trying with this code but no success.

    Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
    btn_refresh.setOnClickListener(new View.OnClickListener() {     
        public void onClick(View v) {
           String current_tab = mTabHost.getCurrentTabTag(); 
           if(current_tab.contains("POPULARE")){                
             Fragment f;
             f = new PopulareActivity(); 
             FragmentTransaction ft =   getSupportFragmentManager().beginTransaction(); 
             ft.replace(R.id.tot,f);               
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
             ft.commit();
           }
           if(current_tab.contains("RECOMANDATE")){           
             Fragment f;
             f = new RecomandateActivity(); 
             FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
             ft.replace(R.id.tot,f);   
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
                         ft.commit(); 
           }
       } 
    });
    

    More exactly I enter on app, press Tab2 (show the list of articles from tab2), press refresh button, open one article, press the back button of mobile device and show me the content of tab2. Now, I enter on tab1 (and show the list of articles from tab1), open one article from the list of tab1, show me what I need, and press the back button from mobile. In this moment is shown the tab2 content (the list of articles form tab2 from where I pressed the refresh button.).

    To open details of an article I use:

    tv.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) {                          
        Fragment f;
        f = new DetaliiActivity();
        Bundle data = new Bundle();  
        data.putInt("id", nid);
        f.setArguments(data);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.tot,f);                     
        ft.addToBackStack(null);                         
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
        ft.commit(); 
      }
    });
    

    Thanks in advance for help!

  • ChuckKelly
    ChuckKelly almost 11 years
    can you elaborate a bit on the refresh method? Im unclear on how exatly this would refresh the fragment still
  • NickL
    NickL almost 11 years
    The refresh method can contain functions to refresh the elements/data etc. in the fragment. It doesn't really do anything with the fragment itself, but it doesn't have to be re-created to update the data in it.
  • ysldl
    ysldl over 9 years
    You can also access the current fragment via ((SectionsPagerAdapter)mViewPager.getAdapter()).getItem(mVie‌​wPager.getCurrentIte‌​m()))
  • Yonatan Nir
    Yonatan Nir almost 9 years
    doesn't work. the tab content remain empty when pressing back and reloading it from the backstack
  • NickL
    NickL almost 9 years
    @YonatanNir I do not see how that is related to the question, which was about a refresh button.
  • Yonatan Nir
    Yonatan Nir almost 9 years
    wanna try answering this: stackoverflow.com/questions/30600634/…
  • Si8
    Si8 over 7 years
    What if I have a view inside the fragment? stackoverflow.com/questions/40095826/…. How can I find the GridView inside the refresh function?
  • Vicky
    Vicky about 6 years
    what if i have the fragment not inside the activity class but in a separate folder class. i am getting NPE for accessing from the activity class.