-3

I am trying to display the table data from SQLite database onto the activity_main.XML. Everything went well. When I am trying to display the same table data onto a fragment of a tabbed activity, I am getting the errors.

I am new to the SQLite database and tabbed activity.

The three fragments are named fragment_one.xml,fragment_two.xml,fragment_three.xml.

I want the SQLite data to be displayed in fragment_two.XML.

The GitHub link for the source code is given below

https://github.com/Nithin543/Team_5_Project/tree/master/Source/app/src/main/java/com/example/cyclone/cyclone_1

Could you please help me to solve this issue.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

It happens because FragmentPagerAdapter loads next/previous fragment in memory for smoother transition and that's why before coming to the foreground your fragment's onCreate(), onCreateView() and onResume() is called. Try in your activity:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch(position){
                    //Do your fragment specific task here
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

You can check my answer http://stackoverflow.com/questions/38428662/when-should-a-fragments-onactivitycreated-be-called/38428898#38428898

Megha Maniar
  • 444
  • 5
  • 22