3

I have [main]Fragment with two ViewPagers in it. Each ViewPager contains several Fragments in it. ViewPager's Fragments obtain data into their ListViews from [main]Fragment. I need to update data in all ListViews, which comes from [main]Fragment.


I've tried to remove current [main]Fragment and add new one (reload it), but when I am calling .remove([main]Fragment) or .detach([main]Fragment) methods in .supportFragmentManager() [main]Fragment is not destroying or detaching, it just hiding and I cant add new one, just make .atach([main]Fragment) for current... Reload activity is bad idea. Please help me.

AskQuestion
  • 346
  • 4
  • 18

1 Answers1

0

Content Observers

Here's a simple URI that the link doesn't show how to do. Put this in the class that does the data changes so any fragment can access it static via ClassName.CONTENT_URI.

private static final String BASE_PATH = "Imustdorefresh";//see content providers
private static final String AUTHORITY = "somethinguniquehereprobablyappcomname";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/" + BASE_PATH);

This code goes in fragments onresume see the link don't forget to unregister in onpause

getContentResolver().
      registerContentObserver(
            CONTENT_URI,
            true,
            yourObserver);

//yourObserver is code to refresh the fragment {do stuff here code}

In the piece of code that changes the data call the contentresolver notify method after every data change. The content resolver will call the registered observers in the fragments.

getContext().getContentResolver().notifyChange(CONTENT_URI,null);

For more information see content provider and content observer. For more details on a Content Provider

danny117
  • 5,581
  • 1
  • 26
  • 35
  • It's wrong solution for me. I must take ArrayList and update it in main fragment, then child fragments take parts of this ArrayList and display them in their listviews, which i need to update. If U know how to do something like that, please help me. – AskQuestion Sep 11 '14 at 18:45
  • Maybe update main fragments arraylist and notify it pager adapter? But when I did it child fragments do nothing... Maybe I did something wrong. – AskQuestion Sep 11 '14 at 18:57
  • This solution works between different apps so it easily works to any level of nested child fragments. The key is the content resolver handles the messaging between provider and observer. – danny117 Sep 11 '14 at 19:58
  • Depending of the new data, ViewPager can make more or less fragments. Maybe there is a way to reload viewpager, pageradapter or main fragment, because I don't think that your method will do this ( – AskQuestion Sep 12 '14 at 04:20
  • @AskQuestion You said I need to update listview that's what this code sample is designed to do. Now you want to update viewpager. You'll have to show us some real code and hopefully I'll update my answer accordingly or a new user will chime in. – danny117 Sep 12 '14 at 10:14