0

I don't know what to put inside "this", "R.id.fragment_container" or "currentFragment" to make the code work. I have one activity with tabs and therefor three Fragments. In this "MainActivity" i want to update the view of a fragment. The code is inside the MainActivity.

Here are two variants of refreshing a fragment by stackoverflow.

In Refresh or force redraw the fragment it says "To refresh ListView you need to call notifyDataSetChanged() on the ListView's adapter." How to do this?

Variant 1

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
if (currentFragment instanceof "NAME OF YOUR FRAGMENT CLASS") {
 FragmentTransaction fragTransaction =   (getActivity()).getFragmentManager().beginTransaction();
 fragTransaction.detach(currentFragment);
 fragTransaction.attach(currentFragment);
 fragTransaction.commit();}
}

Variant 2


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            getFragmentManager().beginTransaction().detach(this).commitNow();
            getFragmentManager().beginTransaction().attach(this).commitNow();
        } else {
            getFragmentManager().beginTransaction().detach(this).attach(this).commit();
        }

Can you help me make the code work? In "this" there is a fragment expected.

Udo
  • 15
  • 3

1 Answers1

0

Try using:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setReorderingAllowed(false);
transaction.detach(fragment).attach(fragment).commitAllowingStateLoss();
  • What do i have to insert in "fragment"? The java-class-filename? – Udo Jul 01 '19 at 16:18
  • Fragment fragment = new FragmentName(); where FragmentName is the name of the fragment you want to open. – Abhishek Kumar Jul 02 '19 at 04:01
  • In https://stackoverflow.com/questions/15262747/refresh-or-force-redraw-the-fragment it says "To refresh ListView you need to call notifyDataSetChanged() on the ListView's adapter." Can you help me doing this? – Udo Jul 02 '19 at 13:30