Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh fragment tab content on button click

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!

like image 941
mr.kosso Avatar asked Nov 29 '12 13:11

mr.kosso


1 Answers

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.

like image 62
NickL Avatar answered Nov 15 '22 20:11

NickL