How to handle AsyncTask's in ActionBarActivity Fragments when ViewPager is used?
Asked Answered
A

4

15

I'm using ActionBarActivity to create 5 tabs. I have used ViewPager to swipe between the 5 tabs using SectionsPagerAdapter which extends FragmentPagerAdapter. Each tab has a fragment with an asynctask called in oncreateview method. When I'm in one fragment, asynctask in other fragment is being called.

I tried using toast messages in oncreateview method is each fragment instead of asynctask. But wrong toast messages are being fired in wrong fragment.

Oncreate method code:

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        mViewPager = (ViewPager) findViewById(R.id.pager_exp); 
        mViewPager.setAdapter(mSectionsPagerAdapter);


        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) 
                    {
                        actionBar.setSelectedNavigationItem(position);
                        actionBar.setTitle(getHomePageTitle(position)); 
                    }
                });


        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) 
        {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setIcon(getPagedrawable(i))
                    .setTabListener(this));

        }

// Adapter class code:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) 
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {


    switch (position) 
    {
        case 0:
            Fragment1 f1 = new Fragment1(); 
            return f1;

        case 1:
            Fragment2 f2 = new Fragment2(); 
            return f2;

        case 2:
            Fragment3 f3 = new Fragment3();  
            return f3;

        case 3: 
            Fragment4 f4 = new Fragment4();
            return f4;


        case 4: 
            Fragment5 f5 = new Fragment5();
            return f5;

        }

        return null;

    }

    @Override
    public int getCount() {
        // Show 5 total pages.
        return 5;  
    } 


}

Fragemnt class code :

public class F1 extends Fragment 
{
@Override
public void onCreate(Bundle savedInstanceState)  
{
    super.onCreate(savedInstanceState);

}

 @SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{ 
    View view       = inflater.inflate(R.layout.f1,container, false);

     Toast.makeText(getActivity(), "F1", Toast.LENGTH_SHORT).show();
    return view;  
}   
}
Aroid answered 20/2, 2014 at 17:9 Comment(0)
E
13

The FragmentPagerAdapter keeps additional fragments, besides the one shown, in resumed state. The solution is to implement a custom OnPageChangeListener and create a new method for when the fragment is shown.

1) Create LifecycleManager Interface The interface will have two methods and each ViewPager’s Fragment will implement it. These methods Are as follows:

public interface FragmentLifecycle {

    public void onPauseFragment();
    public void onResumeFragment();

}

2) Let each Fragment implement the interface Add iplements statement for each class declaration:

public class FragmentBlue extends Fragment implements FragmentLifecycle
public class FragmentGreen extends Fragment implements FragmentLifecycle
public class FragmentPink extends Fragment implements FragmentLifecycle

3) Implement interface methods in each fragment In order to check that it really works as expected, I will just log the method call and show Toast:

@Override
public void onPauseFragment() {
    Log.i(TAG, "onPauseFragment()");
    Toast.makeText(getActivity(), "onPauseFragment():" + TAG, Toast.LENGTH_SHORT).show(); 
}

@Override
public void onResumeFragment() {
    Log.i(TAG, "onResumeFragment()");
    Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show(); 
}

4) Call interface methods on ViewPager page change You can set OnPageChangeListener on ViewPager and get callback each time when ViewPager shows another page:

pager.setOnPageChangeListener(pageChangeListener);

5) Implement OnPageChangeListener to call your custom Lifecycle methods

Listener knows the new position and can call the interface method on new Fragment with the help of PagerAdapter. I can here call onResumeFragment() for new fragment and onPauseFragment() on the current one.

I need to store also the current fragment’s position (initially the current position is equal to 0), since I don’t know whether the user scrolled from left to right or from right to left. See what I mean in code:

private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

    int currentPosition = 0;

    @Override
    public void onPageSelected(int newPosition) {

        FragmentLifecycle fragmentToShow = (FragmentLifecycle)pageAdapter.getItem(newPosition);
        fragmentToShow.onResumeFragment();

        FragmentLifecycle fragmentToHide = (FragmentLifecycle)pageAdapter.getItem(currentPosition);
        fragmentToHide.onPauseFragment();

        currentPosition = newPosition;
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    public void onPageScrollStateChanged(int arg0) { }
};

I didn't write the code. Full tutorial here: http://looksok.wordpress.com/2013/11/02/viewpager-with-detailed-fragment-lifecycle-onresumefragment-including-source-code/

Everara answered 24/6, 2014 at 12:21 Comment(3)
@SomeoneSomewhere the neighboring fragments have to be resumed so you can scroll between them.Everara
I figured it out - the adapter is really important: it needs to instantiate all fragments and keep a reference to them. getItem() then simply returns the object. Unfortunately, I had getItem() create a new Fragment1, Fragment2, etc, each time. But still, using fragments inside a ViewPager is beyond complicated - ridiculously so. The current implementation really only works for a few fragments, otherwise heap usage quickly inflates.Ovenware
@SomeoneSomewhere check out FragmentStatePagerAdapter.This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.Everara
H
1

Use this

viewPager.setOffscreenPageLimit(1); // the number of pages you want to load in background

and also a ProgressDialogue.

Hefner answered 24/6, 2014 at 11:37 Comment(1)
this is not a viable answerOvenware
V
1

ViewPager creates views for fragments adjacent to your current page. This also gives you an opportunity to load any data required for the adjacent fragments. If you are using AsyncTasks to load data, using this feature will result in a better user experience. But if you need an event when a particular page is opened by the viewpager, LordRaydenMK's solution will work.

See my post on the following thread for using AsyncTasks in a ViewPager:

AsyncTask runs on each page of the ViewPager

Vitale answered 28/6, 2014 at 21:46 Comment(0)
C
1

Hi You can try by using tag of each fragment and call method of each fragment on Page Changed

mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                        actionBar.setTitle(getHomePageTitle(position));
                        Fragment f = getFragrmentManager().findFragmentByTag(
                                "" + position);
                        if (f != null)
                            f.refresh();
                    }
                });

and on

         @Override
        public Fragment getItem(int position) {
          Fragment f=null;

        switch (position) 
        {
            case 0:
                f = new Fragment1(); 
                break;

            case 1:
                 f = new Fragment2(); 
                 break;

            case 2:
                 f = new Fragment3();  
                 break;

            case 3: 
                f = new Fragment4();
                break;


            case 4: 
                 f = new Fragment5();
                break;

            }
             f.setTag(""+position);
            return f;

        }
Calc answered 30/6, 2014 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.