android - Refreshing fragments in FragmentActivity after sync service runs -
does have elegant solution refreshing views
in fragments
in fragmentactivity's
viewpager
after sync service
syncadapter
runs?
i've tried calling notifydatasetchanged()
, notifydatasetinvalidated()
on adapter, refreshdrawablestate()
on views (gridviews
), no avail. perhaps i've been calling them wrong places -- i've tried doing @ setuservisiblehint
isvisible
=true, hoping trigger whenever fragment comes view, doesn't work.
i've been using async calls sqlite database data needs, rather content provider
, think have made bit easier. can think of couple of ways without content provider
, neither nice.
any ideas? can provide code if wished. thanks.
i'll assume you're using asynctask loading cursor sake of explanation, work same if you're using loader, threadpool or whatever.
from service, new data changed send localbroadcast
. activity might there or not, broadcast way let know there's new data. service do:
// that's example, let's syncadapter updated album id // create "mybroadcast", you. intent = new intent("albumid_" + albumid); localbroadcastmanager.getinstance(this).sendbroadcast(i);
and activity/fragment have cursor, you'll listening broadcast this:
public void onresume(){ // filter matches broadcast intentfilter filter = new intentfilter("albumid_" + albumid); localbroadcastmanager.getinstance(this).registerreceiver(myreceiver, filter); } public void onpause(){ localbroadcastmanager.getinstance(this).unregisterreceiver(myreceiver); } // , of course have create broadcastreceiver private broadcastreceiver myreceiver = new broadcastreceiver(){ @override public void onreceive(context context, intent intent){ // here know data have changed, it's time reload reloaddata = new reloaddata().execute(); // should cancel task onpause() } };
as said, next part varies depending on threading method you're using load cursor, example i'll show in asynctask because it's popular (but believe , every developer in world should use loaders pattern).
private class reloaddata extends asynctask<void, void, cursor> { protected cursor doinbackground(void... void) { // here query data base , return new cursor ... query ... return cursor; } protected void onpostexecute(cursor result) { // said you're using subclass of cursoradater // have method changecursor, changes cursor , closes old 1 myadapter.changecursor(result); }
}
the above approach tested , used before , know works. there's way of making work flag flag_register_content_observer
, override oncontentchanged()
re-execute query , swap cursor, i've never tested it. that:
init adapter constructor cursoradapter(context context, cursor c, int flags)
passing flag flag_register_content_observer
, override oncontentchanged()
. inside oncontentchanged execute asynctask above. way don't have use localbroadcastmanager
database alert. reason method not main answer, it's because i've never tested it.
note autorequery
have been deprecated , it's discouraged performs data loading in ui thread.
edit:
i noticed content observer api 11 thing. have 2 options: 1 use support library instead: https://developer.android.com/reference/android/support/v4/widget/cursoradapter.html or broadcast option.
Comments
Post a Comment