How to update the ListView of a Fragment from another Fragment?

You can access another Fragment by its tag:

 // find your fragment
 YourFragment f = (YourFragment) getSupportFragmentManager().findFragmentByTag("yourFragTag");

 // update the list view
 f.updateListView(); 

The tag of your Fragment is set when it is attached to a container layout:

FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.frameBuy, YourFragment.newInstance(), "yourFragTag").commit();

So when you click your Button, find the Fragment you want to refresh by its tag, and then call your refresh method.

IF you are using a ViewPager, this is how to get the Fragments tag:

   /**
     * Gets the fragment tag of a fragment at a specific position in the viewpager.
     *
     * @param pos the pos
     * @return the fragment tag
     */
    public String getFragmentTag(int pos){
        return "android:switcher:"+R.id.yourViewPagerId+":"+pos;
    }
Share:
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin about 2 months

Related