简体   繁体   中英

Refreshing android fragment after resume

I have a Fragment in my MainActivity with ListView the content of which is browsed from a database. The method getUpdates() gets the data from database and then that data is applied to ListView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...
    ...
    getUpdates();
    View v = inflater.inflate(R.layout.fragment_test_list, container , false);
    ListView lv = (ListView) v.findViewById(R.id.testListView);
    ...
    return v;
}

When other Activity is launched the database is changed, but when the activity is closed and the MainActivity is resumed I cant see the updates in the Fragment. So the onCreateView method is not called. But I need to update the View of fragment in onResume() method. How can I do that? Or if there is another more efficient way with using listeners , please give me to know.

Your fragments that rely on these events could implement an interface that your activity can call (or you can have a base fragment that provides the overridable function). When your activity needs to trigger a refresh on your Fragment, just call the function and handle it in the Fragment.

You could do it with broadcasts, but that seems a bit overkill if the notifications aren't coming from a service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM