Search code examples
androidandroid-fragmentsandroid-tabs

Update bundle data of tabs frgament onResume


i can't solve this problem.

I have one activity : PdvActivity with a viewPager. This activity gets a list of data and set this data to the TabsPagerAdapter. TabsPagerAdapter create 3 tabs fragments and pass the data in their bundle. The first tab has a gridView with items from the list he received from the bundle. I click on an item and an activity start ... when activity finish i need to refresh this grid.

I did :

PdvActivity onResume : i read the fresh data, i set the data in the adapter and i set again the adapter to the pager

Panel[] panels = DB.getPdvPanels(pdv.id, true, activity_list);
Company company = DB.getCompany(panels[0].attivita.company_id);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mAdapter.setPdv(pdv);
mAdapter.setActivities(panels);
mAdapter.setCompany(company);
viewPager.setAdapter(mAdapter);

In the first Tab onResume :

@Override
public void onResume() {
    super.onResume();
    Panel[] array_panel = (Panel[])getArguments().getParcelableArray("attivita");
    gridview = (GridView)rootView.findViewById(R.id.gridView);
    gridview.setAdapter(new AttivitaPdvAdapter(getActivity(), array_panel));
    gridview.invalidateViews();
}

PdvActivity gets the fresh data, but the grid refresh itself with old data from bundle. And nothing change.

What i am missing ?

Thanks in advice


Solution

  • I could not find a way to update the bundle. So i changed my code in a way that i think is a workaround.

    Instead of refresh data in the main activity i moved that piece into the fragment. Now in the bundle i pass only the arguments needed to do the query and i instantiate the DB inside the fragment to do the query.

    @Override
    public void onResume() {
        super.onResume();
        Panel[] array_panel =   (Panel[])getArguments().getParcelableArray("attivita");
        gridview = (GridView)rootView.findViewById(R.id.gridView);
        gridview.setAdapter(new AttivitaPdvAdapter(getActivity(), array_panel));
        gridview.invalidateViews();
    }
    

    to that :

    @Override
    public void onResume() {
        super.onResume();
        ArrayList<Attivita> activity_list = (ArrayList<Attivita>)getArguments().get("attivita");
        panels = DB.getPdvPanels(pdv.id, true, activity_list);
        gridview = (GridView)rootView.findViewById(R.id.gridView);
        gridview.setAdapter(new AttivitaPdvAdapter(getActivity(), panels));
        gridview.invalidateViews();
    }
    

    I dont know if is a good solution, or if there is a better way. But it works, and it seems ok for me.

    Hope it will helps you.