Samantha Withanage Samantha Withanage - 1 year ago 169
Android Question

refresh fragment at reload

In an android application I'm loading data from a db into a table view inside a fragment. But when I reload the fragment it displays the previous data. Can I repopulate the fragment with current data instead of previous data?

Answer Source

I think you want to refresh the fragment contents upon db update

If so, detach the fragment and reattach it

// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

Your_Fragment_TAG is the name you gave your fragment when you created it

This code is for support library.

If you're not supporting older devices, just use getFragmentManager instead of getSupportFragmentManager


This method requires the Fragment to have a tag.
In case you don't have it, then @Hammer's method is what you need.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download