Reload or notify Fragment's data in ViewPager
Asked Answered
I

1

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.

Impudent answered 11/9, 2014 at 15:37 Comment(3)
did you used childFragmentManager ?Eri
This is a difficult question because it takes so many forms and can be done so many ways. I have included a content observer solution below because you have a neato userid.Procambium
Akhil, yes. For all child fragments.Impudent
P
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

Procambium answered 11/9, 2014 at 18:28 Comment(5)
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.Impudent
Maybe update main fragments arraylist and notify it pager adapter? But when I did it child fragments do nothing... Maybe I did something wrong.Impudent
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.Procambium
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 (Impudent
@Impudent 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.Procambium

© 2022 - 2024 — McMap. All rights reserved.