0

I need help with reloading a Fragment that is part of a ViewPager. Here is my current setup: ReportActivity extends FragmentActivity and uses ViewPager to host 4 Tabs TAB 1 to 4. Each Tab is a Fragment with seperate layout.

ViewPager uses TabsPagerAdapter to switch between the tabs and this works as expected.

TAB1Fragment displays a graph using a static dataset. Now I have added a spinner to dynamically change the dataset and I am looking for ways to reload the Fragment so it can re-display the graph with the correct dataset. The following ints correspond to the values of the Spinner.

    static final int REPORT_PERIOD_DAY = 0;
    static final int REPORT_PERIOD_WEEK = 1;
    static final int REPORT_PERIOD_MONTH = 2;
    static final int REPORT_PERIOD_YEAR = 3;

I have a method that correctly calculates the correct data based on the the int passed in, what I am struggling with is how to create an Intent or any other method that will recreate TAB1Fragment and passes it an int parameter.

Here is where I want to re-create the Fragment

        mReportPeriodSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0:
                    //How can I re-load the Fragment here?
                    reloadFragment(REPORT_PERIOD_DAY);
                    break;
                case 1:
                    reloadFragment(REPORT_PERIOD_WEEK);
                    break;
                case 2:
                    reloadFragment(REPORT_PERIOD_MONTH);
                    break;
                case 3:
                    reloadFragment(REPORT_PERIOD_YEAR);
                    break;
            }

        }

Please can you give me an idea of how to write the code that goes into the method reloadFragment(Int period)

Val Okafor
  • 3,043
  • 12
  • 39
  • 66

1 Answers1

1

I would recommend re-configuring the existing fragment, rather than constructing a new fragment with the new report period.

I'm guessing that you currently configure your graph inside TAB1Fragment's onCreateView. If you move that configuration code to onResume, then you can call the same function when the user selects something in the spinner.

public class TAB1Fragment extends Fragment {
    int reportPeriod;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = ...
        reportPeriod = REPORT_PERIOD_DAY; // default report period
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        configureGraph();
    }

    private void configureGraph() {
        //TODO put all the code that draws your graphs here, using reportPeriod
    }

    public void reloadFragment(int reportPeriod) {
        this.reportPeriod = reportPeriod;
        configureGraph();
    }
}

If this approach really doesn't work for you, and you really need a new fragment each time, then this question explains how to get the tag for a fragment so that you can tell the FragmentManager to replace it: Replace Fragment inside a ViewPager

Community
  • 1
  • 1
Bruce
  • 2,317
  • 1
  • 14
  • 11
  • Great answer, it almost worked but the graph is not refreshed, I am able to load the correct data set using your approach. I will continue to investigate. Thanks for the answer – Val Okafor Dec 02 '14 at 18:04