0

i have below code for data remove from DB. So i delete data but i want to update fragment from here.

how i can refesh fragment ?

 // Remove entry from database table
                database.delete(FAVORITES_TABLE, FAVORITES_ITEM_ID + " = " + soundObject.getItemID(), null);

// Refesh current fragment 
????????????????????????????????

this is code which i use in Fragment to refesh it (on button press) work good, but i don't want to tap refesh button to refesh page. I want fragment refesh when DB change by it self.

 public void refersh()
    {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.detach(this).attach(this).commit();
    }

any idea? Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
buba
  • 1
  • 3

4 Answers4

0

you are detaching and attaching again in one commit, so it does nothing. make it two commits

public void refersh(){
    FragmentTransaction ft1 = getFragmentManager().beginTransaction();
    ft1.detach(this).commit();
    FragmentTransaction ft2 = getFragmentManager().beginTransaction();
    ft2.attach(this).commit();
}

there is also possibility that you shoud use getSupportFragmentManager instead of getFragmentManager

its probably not best/most-efficient solution, but you posted ony this small snippet...

snachmsm
  • 17,866
  • 3
  • 32
  • 74
0

In simple terms you can use:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Gagan
  • 745
  • 10
  • 31
  • but only if im calling this fucntion from huntle. but i have evenhandlerclass and i want to call it from there. – buba Jun 19 '17 at 12:32
0

I think you want to refresh the fragment contents upon DB data updates

If so, detach the fragment and again attach it

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

Fragment_TAG is the name you gave your fragment when you created it

Maraj Hussain
  • 1,580
  • 10
  • 26
  • ok, so i have item menu with remove file action. (when i tap remove file is removed from DB, but fragment is not refeshed) where i should put your code? thx – buba Jun 19 '17 at 12:38
  • When you calling the remove file operation from fragment then call the above code from there. – Maraj Hussain Jun 19 '17 at 12:55
  • `int android.support.v4.app.Fragment.mContainerId' on a null object reference at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1017)` getting this error :( – buba Jun 19 '17 at 13:30
  • please check this [link](https://stackoverflow.com/questions/22489703/trying-to-remove-fragment-from-view-gives-me-nullpointerexception-on-mnextanim) for your error may be this help you out... (Y) – Maraj Hussain Jun 20 '17 at 06:26
0

To refresh the current fragment content, you can use the following code inside the method refresh()

public void refresh()
   {
            Fragment fragment = null;
            fragment = new Current_Fragment_Name(); //replace the  Current_Fragment_Name with your current fragment name
            FragmentTransaction ft =  getActivity().getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
   }

Hope it works for you.

Subrata
  • 182
  • 7