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....
you have to call the onPageSelected() after initialization of the adapter and setAdapter() otherwise adapter will return null always
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With