Method to Refresh Fragment Content When Data Changed ( Like Recall Oncreateview)

Method to refresh Fragment content when data changed ( like recall onCreateView)

Detach and attach it with

Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();

or search fragment with

Fragment currentFragment = getActivity().getSupportFragmentManager().findFragmentById(R.id.container);

Refresh Fragment at reload

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();
ft.detach(frg);
ft.attach(frg);
ft.commit();

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

[EDIT]

This method requires the Fragment to have a tag.

In case you don't have it, then @Hammer's method is what you need.

How to update a Fragment that has a Gridview populated from Sqlite

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frag).attach(frag).commit(); //frag is my Fragment instance...

Each time the dialog closed and it did the trick... simple and easy!

How to refresh TabbedActivity's View from onRestart method | How to access fragment's view element from other class

So you have a list in a Fragment that is inside a ViewPager. Ther are a few ways to solve your problem, I'd suggest you use the first one, because it is the simplest.

  1. Fragment have lifecycle callbacks that are triggered together with lifecycle callbacks of Activity that contains that fragment. It covers most of the methods, but unfortunately not the onRestart that you are looking for. But that's not a big problem. Actually, you almost never need onRestart because you have onStart method, that method is accessible from both Fragments and Activities. It is called every time activity is restarted + the very first time activity is started. And as we can see it is exactly what you need. So to have your list updated every time, just remove the update code from the onCreate method and put it into onStart method of the Fragment.

    public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    private ListView listView = null;

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
    PlaceholderFragment fragment = new PlaceholderFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);

    int scnum = getArguments().getInt(ARG_SECTION_NUMBER);

    listView = (ListView) rootView.findViewById(R.id.products_list);

    return rootView;
    }

    @Override
    public void onStart() {
    // here refresh your listView trough internet using the listView class field
    }
    }
  2. Another option would be to get a reference to your fragment inside the activity. The solution I usually use is to retain a reference to the fragment inside the adapter. It is similar to the second option in your question. The only problem with your solution is that mSectionsPagerAdapter.getItem(0); always create a new fragment, while you need to get a reference to already existing fragment.

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    private PlaceholderFragment fragmentZero = null;

    public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
    }

    @Override
    public Fragment getItem(int position) {
    PlaceholderFragment tabFragment = PlaceholderFragment.newInstance(position + 1);

    if (position == 0) {
    fragmentZero = tabFragment;
    }

    return tabFragment;
    }

    @Override
    public int getCount() {
    return 3;
    }

    public PlaceholderFragment getFragmentZero() {
    return fragmentZero;
    }
    }

    You also need to move your list update logic to a separate method in the PlaceholderFragment:

    private ListView listView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_list_view, container, false);

    int scnum = getArguments().getInt(ARG_SECTION_NUMBER);

    listView = (ListView) rootView.findViewById(R.id.products_list);

    refreshListView();

    return rootView;
    }

    public void refreshListView() {
    // here refresh your listView trough internet
    }

    At this point you can do:

    @Override
    protected void onRestart() {
    super.onRestart();

    Fragment listFragment = mSectionsPagerAdapter.getFragmentZero();

    listFragment.refreshListView();
    }
  3. Whenever you have a fragment inside an activity, in a ViewPager or not, you can get a reference to the fragment using FragmentManager. Check this for details regarding the ViewPager option.

All the code above is rather straightforward and would not require any special Kotlin idioms if you'd prefer to implement it in Kotlin. You can start with converting your classes in the android studio. To do it:

  • Navigate to your java class
  • Press Control + Shift + A on Windows or Command + Shift + A on Mac
  • Search for Convert Java File to Kotlin File
  • Apply

And you are good to go with the Kotlin code of given classes. The only thing that might be quite different from java is handling ARG_SECTION_NUMBER since Kotlin does not have static fields and use Companion Objects instead.



Related Topics



Leave a reply



Submit