Android TabsAdapter with ActionbarSherlock

被刻印的时光 ゝ 提交于 2019-12-05 12:36:50

Attempting to retrieve the loader here is simply the wrong idea, as the purpose is to refresh the data. After doing some digging I found that I can notify more than 1 set of content load in my provider using

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

which notifies it of change and refreshes, all this code can be deleted and replaced with a single line.

If you're still using the Activity's LoaderManager to manage your Fragments loaders... don't do that. The Activity's LoaderManager manages loaders across your Activity's* lifecycle... not your Fragment's. Your Fragment is probably trying to access a Loader that the Activity has not initialized yet.

I had the same exact problem a couple of days ago, during an hackaton :-)

It turns out that getItem() does not return the fragment you created, but instantiate a new one. Basically that is the method that get called to initially create the fragments. That's why you are finding its member empty.

I was a bit in hurry, but I think with that solution there is no way to access the fragments from the activity.

However, the workaround that worked for me was to set a reference to the fragments in the activity when they were created.

Something like

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((ApplicationActivity)getActivity()).setEventListFragment(this);
}

inside your fragment and

EventListFragment getEventListFragment(){
    return mEventListFragment;
}

public void setEventListFragment(EventListFragment m){
    mEventListFragment = m;
}

inside your activity.

You should then use getEventListFragment() instead of using the getItem() method of the adapter.

If you want to see the whole code I wrote (and especially the part you need), you can check it here

https://github.com/moodeque/moodeque-android/tree/master/src/main/java/com/whiterabbit/hackitaly/Activities

The container activity is InVenueActivity , whereas the two fragments are those contained in the tabs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!