I am updating an application that was written in 2014. The fragment that I would like to update (refresh) uses the following code,
public void fragmentRefresh(View view) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (Build.VERSION.SDK_INT >= 26) {
ft.setReorderingAllowed(false);
}
ft.detach(this).attach(this).addToBackStack(null).commit();
}
The application does not display the updated fragment until I load another tab and then browse back. A sample application has been created with four tabs to investigate (and illustrate) this issue (see Figure 1 below).
This example application is available on GitHub here: https://github.com/portsample/moderntablayout
Question(s): The version of the application that I wrote in 2014 (not the example on Github) would update the view after a fragment swap using the old version of ViewPager using methods similar to those presented here with no problem. Is the current issue a function of this method diverging from Google "best practices" and not fully supported by ViewPager2 and the other updates? What is the best remedy, (i.e. most widely accepted solution) for this issue?
Thanks in advance for cogent and constructive responses.

I believe I had the same issue before, I solved it by using the onResume() and onPause() methods, you can read about them in fragment lifecycle
so what you have to do is to create a function for what you want to refresh instead of attaching and detaching the fragment. in my example I was creating button and getting their data from my database so instead of the attach method, I call the function and every time the user add something to the database, and return to the fragment it will automatically refresh because of the onResume()
example: