Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:52:40+00:00 2026-06-14T14:52:40+00:00

onCreateView for my first fragment in the FragmentPagerAdapter is not very quick. So, changing

  • 0

onCreateView for my first fragment in the FragmentPagerAdapter is not very quick.
So, changing curent tab to the first has a delay.

How to disable recreate first Fragment in FragmentPagerAdapter on tab Selected?

private class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
    private final Context context;
    private ActionBar bar;
    private final ViewPager viewPager;
    private final ArrayList<TabInfo> tabs = new ArrayList<TabInfo>();

    final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        context = activity;
        bar = activity.getSupportActionBar();
        viewPager = pager;
        viewPager.setAdapter(this);
        viewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        tabs.add(info);
        bar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = tabs.get(position);
        Fragment fragment = Fragment.instantiate(context, info.clss.getName(), info.args);
        return fragment;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    @Override
    public void onPageScrollStateChanged(int state) {}

    @Override
    public void onPageSelected(int position) {
        bar.setSelectedNavigationItem(position);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < tabs.size(); i++) {
            if (tabs.get(i) == tag) 
                viewPager.setCurrentItem(i);
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {}
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:52:41+00:00Added an answer on June 14, 2026 at 2:52 pm

    Right now i am stuck with the same problem.

    when you are on a Tab:(n), only Tab:(n-1) and Tab:(n+1) will be alive in the memory, for memory usage optimization. Rest all Tabs will be destroyed, thats the reason why when you come back to the first Tab, its onCreateView is being called again.

    Actually Tab:1’s onCreateView will be called even if you click Tab:2 because its the neighbourhood Tab.

    One solution i got is:

    change the OffscreenPageLimit of the ViewPager. Its default value is 1

    viewPager.setOffscreenPageLimit(total no of Tabs - 1);
    

    This way all the Tabs will be alive even if its offScreen. But this is recommended only if you have decent no of Tabs(<=5)

    If you have huge no.of Tabs, better use

    FragmentStatePagerAdapter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fragment embedded in a layout. This fragment has a very simple
I want to inflate a LinearLayout into a fragment in the onCreateView method. But
I have a system of actionbar Tabs with ABS. The first tab is a
I am developing Android v2.2 app. I have a Fragment . In the onCreateView(...)
I'm not quite understanding this fragment lifecycle business. I have a pretty standard 3
I have an animation which starts correctly the first time the fragment is displayed.
My problems is that onCreateView is not called in my MapFragment to reinstantiate/create the
I've got a Fragment whose onCreateView() callback returns a custom View . The View
Using fragments for the first time. As fragment in itself an activity, going by
I am trying to create an activity with 2 Fragment First fragment show a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.