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 8938279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:35:21+00:00 2026-06-15T10:35:21+00:00

Is my following Action bar Tab implementation method is efficient or not? Because whenever

  • 0

Is my following Action bar Tab implementation method is efficient or not? Because whenever i switch between tabs the Tab content fragments are replaced and load again in frame layout.

I want to add fragments at first time itself and show the fragments smoothly(without reloading) when switch between tabs.

Note: i tried to add all fragments on onCreate method.But the fragments are overlapped with each other and display all fragments in frame layout at same time.

My Code:

public class ManageActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = getSupportActionBar().newTab();
        tab1.setText("Tab1");
        tab1.setTabListener(this);
        getSupportActionBar().addTab(tab1);

        ActionBar.Tab tab2 = getSupportActionBar().newTab();
        tab2.setText("Tab2");
        tab2.setTabListener(this);
        getSupportActionBar().addTab(tab2);

        ActionBar.Tab tab3 = getSupportActionBar().newTab();
        tab3.setText("Tab3");
        tab3.setTabListener(this);
        getSupportActionBar().addTab(tab3);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        String selectedtab = tab.getText().toString();

        if(selectedtab.equalsIgnoreCase("Tab1"))
        {
            getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab1Fragment()).commit();
        }
        else if (selectedtab.equalsIgnoreCase("Tab2")) {
            getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab2Fragment()).commit();
        } 
        else if (selectedtab.equalsIgnoreCase("Tab3")){
            getSupportFragmentManager().beginTransaction().replace(R.id.tabfragment_container, new Tab3Fragment()).commit();
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}

activity_manage.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tabfragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ManageActivity" >
</FrameLayout>

Note: My Tab contents are Listfragments which query data from Sqlite using loader manager.

  • 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-15T10:35:22+00:00Added an answer on June 15, 2026 at 10:35 am

    I use a ViewPager that contains all my fragments. Every time a tab is selected I only switch the position in the Viewpager.

       @Override
       public void onTabSelected(Tab tab, FragmentTransaction ft) {
          int position = adapter.findItemPosition(this.id);
          ViewPager viewPager = (YourActivity)getActivity().getViewPager(); // Convenience method
          if (viewPager != null) {
             viewPager.setCurrentItem(position, this.shouldScroll);
          } else {
             Log.d(getClass().getSimpleName(), "No pager available");
          }
       }
    

    To make this code working you need a Viewpager and have to add all tabs into the ViewPager through an instance of PagerAdapter. I use a FragmentPagerAdapter as there are not that many tabs most of the time.

    This also enables your user to swipe through the tabs. If the user changes the selected tab through swiping you need to update the selected tab. I use ActionBarSherlock in all of my Apps. Therefore the next Snippet uses a supportActionBar. This code example is a simple listener that can be set on the ViewPager to update tab changes through swiping.

       private final class TabPageChangedListener extends ViewPager.SimpleOnPageChangeListener {
    
          @Override
          public void onPageSelected(int position) {
             ActionBar actionBar = getSupportActionBar();
             if (actionBar != null) {
                actionBar.setSelectedNavigationItem(position);
             } else {
                Log.e(getClass().getSimpleName(),
                      "No actionbar available to change selected tab.");
             }
          }
       }
    

    There is also the possibility to disable swiping by overwriting the ViewPager and intercepting the swiping touch motions if you do not want the tabs to be swipeable.

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

Sidebar

Related Questions

I have successfully set up tabs in the Action Bar using the following example
I have the following action method: [HttpGet, Authorize, OutputCache(Duration = 60, VaryByHeader = Cookie,
I'm exploring alfresco action tag. Following action tag does not specify permission <action id=document-copy-to
I'm adding navigation tabs to an action bar using the sample code provided here:
If I define the following items for my action bar: res/menu/action_menu.xml : <?xml version=1.0
I have the following problem. On my layout I have an action bar (which
I've got a Honeycomb app that uses fragments and an action bar. I'd like
I have an activity with the following items: Action bar (one imageview, one view
And want to create tabs WITHOUT using Action bar. What will be the best
Current, I have 2 Fragments , which is switch-able through ActionBar 's tab. getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

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.