Search code examples
androidandroid-fragmentssimplecursoradapter

How do I update a fragment from the parent activity when it is managed in a ViewPager?


I have an activity that stores NFC reads into my apps database. I have a fragment that has a ListView with data bound by a cursor adapter to that same database.

The activity handles the intent in the foreground but when I swipe to the fragment, the listview has not been updated with the databases latest data.

What is the best way to tell the fragment to update the listview when my activity adds the NFC read to the database?

The fragment is being managed in the activity by a viewpager, so I can't get access to the fragment with getFragmentById() because I don't have an ID. I've read some weird android:switcher hack to access the ID or Tag of the fragment but that doesn't seem like a good idea.


Solution

  • 3 possible ways:

    1. Send a broadcast from enclosing activity and have your fragment listen and act on that broadcast
    2. Implement a callback mechanism e.g. http://www.javaworld.com/javatips/jw-javatip10.html
    3. Provide a public method in your Fragment that can be called by the enclosing activity