0

I have a class that is handling a database in my Android app. When the database changes, I'd like to update the fragment displaying the information from the frogment. My approach has been to give the fragment a tag and then find the fragment with the following code:

FragmentManager manager = getFragmentManager();
        Fragment fragment = manager.findFragmentByTag("Schedule");
        if (fragment instanceof ScheduleFragment){
            ScheduleFragment fr = (ScheduleFragment)fragment;
            fr.scheduleUpdated();
        }

However, as long as my database class is not an extension of Fragment, the compiler refuses to recognise getFragmentManager(). To me it makes no sense to extend Fragment, as the database class is no fragment, but a simple helper class to manage the database. Is it possible to get a reference to my fragment without extending Fragment? Or is this bad practice and should be done in another way?

Also, is it possible to get a reference to the fragment from a static method?

Jorn
  • 1,054
  • 9
  • 25

1 Answers1

1

try using a localBroadcast manager. when database changes laumch a Broadcast intent. registere this in "Schedule" Fragment and you can handle the database changes. Refer to this link for more about LocalBroadcast Manager how to use LocalBroadcastManager? http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

Community
  • 1
  • 1
NIPHIN
  • 1,071
  • 1
  • 8
  • 16