0

I am using a Fragment class. I add it using

  FragmentTransaction fragmentTransaction =      fragmentManager.beginTransaction();


         Search fragment = new  Search(maincontrolActivity.this);

          fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
          fragmentTransaction.commit();
          this.FragmentObject = fragment 

when I do refresh the control,I recall this code but by passing this.FragmentObject but I think it be garbage collected because the = refere to the same object , and when say add, it free the old fragement which is the same

so do I need a deep copy or any way to refresh ?

any idea

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
AMH
  • 6,363
  • 27
  • 84
  • 135
  • I'd say you are approaching the problem incorrectly. Why not have a `refreshData` method inside your fragment that causes it to refresh its `View`s and repopulate the data? Instead of recreating the entire fragment which causes a lot of overhead... – Salil Pandit Jul 22 '12 at 19:08
  • @SalilPandit could u give me example – AMH Jul 22 '12 at 19:13
  • the problem is that I have many fragments , u mean in each fragment ,I put my datarefresher method or something like that – AMH Jul 22 '12 at 19:21
  • Yeah thats an option. am I right in guessing you're applying some kind of filter that is requiring the data to be updated? Maybe a location or something? – Salil Pandit Jul 22 '12 at 19:23
  • no I have main scree nthat contains buttons and menue, so when click I fill layout with the desired fragment, the custoemr askemd me to put refresh button on the main screen such that when click the button , I refresh the fragment , the problem is that the fragments is different in the contained data , so I thought about reload the fragment – AMH Jul 22 '12 at 19:26
  • Is the refresh button in the `Activity` layout or the `Fragment` layout? (this will help me give you a code example...) – Salil Pandit Jul 22 '12 at 22:41
  • @SalilPandit the refresh button in the Activity layout – AMH Jul 23 '12 at 02:07

1 Answers1

1

Ok. So what I would do is have an Interface defined and have each Fragment register with the Activity for a callback when the refresh button is clicked. Then in the fragment itself have it refresh its data. If that means getting new data, switch to a ProgressBar, get the data from the server, and repopulate the Views. Here is an entire article on creating Interfaces in Activities and calling Fragments from them. Here is roughly what your code will look like... The Activity:

public class RefreshActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.refresh_button).setOnClickListener(this);
    }

    public interface OnRefreshPressedListener {
        public void onRefreshPressed();
    }

    @Override
    public void onClick(View v) {
        ((OnRefreshPressedListener)this.FragmentObject).onRefreshPressed();
    }
}

The Fragment:

public class Search extends Fragment implements OnRefreshPressedListener {

    @Override
    public void onRefreshPressed() {
        //TODO: Refresh your data!

    }

}
Salil Pandit
  • 1,498
  • 10
  • 13