Reputation: 2409
I use a FragmentPagerAdapter to switch from fragments. I need some functions to be called when a fragmentswitch is made and had some troubles with OnPause and OnResume, so as suggested by THIS question I have implemented an interface OnPageSelectListener :
public interface OnPageSelectListener {
void onPageSelected();
void onPageNotVisible();
}
It calls the function OnPageSelected whenever this page comes to the foreground. Works nice, except that I want to call a function on my adapter. I thought that would work, except that my adapter returns NULL all the times (even though it is initialized and data is loaded in my listview as prefered).
public class AfterCheckFragment extends Fragment implements OnPageSelectListener{
private ListView listView;
private List<Check> checkList;
private CheckListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_check, container, false);
System.out.println("VIEW create called");
//(.. some other stuff, not relevant for question..)
//initializing the adapter
listView = (ListView) view.findViewById(R.id.listView);
adapter = new CheckListAdapter(checkList,getActivity(),trainPosition);
listView.setAdapter(adapter);
adapter.handleButtonVisibility();
return view;
}
@Override
public void onPageSelected() {
if(this.adapter != null) {
System.out.println("adapter not null");
this.adapter.checkForActive();
}else{
System.out.println("Adapter is NULL");
}
}
@Override
public void onPageNotVisible() { //page is moved to backgroung
System.out.println("AFTER not active any more ");
}
}
Now is my question: Why does adapter (or any other object in the fragment) return null when I return to my fragment? When the fragmentPager is initialized the onActivityCreate function of the fragment is called one time, but after that not any more, and the adapter return null....
Upvotes: 6
Views: 2538
Reputation: 62519
for me i had to call this on my viewpager:
myViewPager.setSaveFromParentEnabled(false);
Upvotes: 1
Reputation: 2257
Here is why I think your CheckListAdapter
(i'll call it listAdapter
) is null:
pagerAdapter
to the ViewPager
ViewPager
asks the pagerAdapter
for a new Fragment
ViewPager
tells the FragmentManager
to use itonPageSelected
gets calledlistAdapter
. It hasn't been initialized yet at this point. (NPE)FragmentManager
drags the Fragment through all its stages.onCreateView
gets called. Your listAdapter
is created.Don't try and use internal data of a fragment outside of it. It is meant to work as a standalone unit, it won't be very good if you use it differently. Since the fragment is initialized at a later stage, you can't use it like you intend.
You can try and do what you want to do in the fragment, rather than the pagerAdapter
, or write a method in the hosting Activity
and call it from the fragment when ready, or even launch an event.
Upvotes: 2
Reputation: 4030
Why don't you use a viewpager.addOnPageChangeListener
, in you pager , after setting its adapter and the setOffscreenPageLimit()
instead of implements
it on your fragment?
Heres a sample code:
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
Make it in your Activity
, where you setup your ViewPager
, of course.
Upvotes: 1
Reputation: 2263
This is because Viewpager calls OnpageSelected way before Fragments in oncreateView()/onActivityCreated() is called .
The best way for you is to inflate your views in the constructor of the Fragment and set the Adapters.
Or
Use a member variable to store whether the Fragment is active or not. And use the variable in oncreateview() to call function on your adapter.
Upvotes: 1
Reputation: 271
you have to call the onPageSelected() after initialization of the adapter and setAdapter() otherwise adapter will return null always
Upvotes: 3
Reputation: 30985
ViewPager
will create and destroy fragments as the user changes pages (see ViewPager.setOffscreenPageLimit()
). So onActivityCreated()
is only called on the fragment when it is being restored or set up for the first time. Hence, fragments can be created without ever having onActivityCreated()
called.
Instead of onActivityCreated()
, I would recommend overriding onViewCreated()
and setting up your adapter there. No fragment can be displayed without having a view created, so this is a good place to do that kind of stuff.
If you have your OnPageSelectListener
logic working, that's good. I found the best way to know when your fragment is actually in front of the user is by overriding setPrimaryItem()
in the FragmentPagerAdapter
. Getting the page out of view event is a little trickier, since you have to keep a reference to the fragment from the previous setPrimaryItem()
call.
Upvotes: 2