Search code examples
androidandroid-fragmentsandroid-viewpageroncreatefragmentpageradapter

Fragment in ViewPager returns empty object onResume


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....


Solution

  • Here is why I think your CheckListAdapter (i'll call it listAdapter) is null:

    1. You give the pagerAdapter to the ViewPager
    2. The ViewPager asks the pagerAdapter for a new Fragment
    3. The ViewPager tells the FragmentManager to use it
    4. onPageSelected gets called
    5. You try and use listAdapter. It hasn't been initialized yet at this point. (NPE)
    6. The FragmentManager drags the Fragment through all its stages.
    7. 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.