Organizational Research By

Surprising Reserch Topic

how to handle asynctasks in actionbaractivity fragments when viewpager is used using -'android,android-asynctask,android-actionbar-compat,android-actionbaractivity,quora'

how to handle asynctasks in actionbaractivity fragments when viewpager is used  using -'android,android-asynctask,android-actionbar-compat,android-actionbaractivity,quora'

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(;

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    public void onPageSelected(int 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.


// Adapter class code:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm)

    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;


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


Fragemnt class code :

public class F1 extends Fragment
public void onCreate(Bundle savedInstanceState)  


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;  


asked Sep 7, 2015 by rajesh
0 votes

Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.