Kotlin Refresh Fragment on Reload (KRFR)


KRFR:   SOLD       




In my android application I am loading data from Db to TableView inside Fragment. But when I reload the Fragment it will show previous data. Can I repopulate the Fragment with current data instead of previous data?

I guess you want to refresh the fragment content after db update, if so then just detach the fragment and reattach it again.

Example:

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

The code above is just an example, please adjust the rest yourself to the case you are facing, of course this is related to how you implement the fragment.

Your_Fragment_TAG is the name you gave your fragment when you created it. This code is for the android support library. If your project is still using the old code, then just use getFragmentManager instead of getSupportFragmentManager, because this method requires a Tag for a Fragment.


Post a Comment

Previous Next

نموذج الاتصال