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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:42:02+00:00 2026-06-01T15:42:02+00:00

Using this sample : I have make my own Fragment that holds tabhost and

  • 0

Using this sample: I have make my own Fragment that holds tabhost and tab content:

    public class TabsFragment extends Fragment
    {
        private static final String EXTRA_TAB = "EXTRA_TAB";

        private TabHost tabHost;
        private TabManager tabManager;
        private MaptrixFragmentActivity activity;
        private Context context;

        @Override
        public void onAttach(Activity activity) 
        {
            this.activity = (MaptrixFragmentActivity) activity;
            this.context = activity;
            super.onAttach(activity);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {
            View view = inflater.inflate(R.layout.fragment_tabs, container, false);
            tabHost = (TabHost)view.findViewById(android.R.id.tabhost);
            tabHost.setup();
            tabManager = new TabManager(activity, tabHost, R.id.realtabcontent);

            initTabs();

            if (savedInstanceState != null)
            {
                tabHost.setCurrentTabByTag(savedInstanceState.getString(EXTRA_TAB));
            }
            return view;
        }

        private void initTabs()
        {   
            // tab one
            addTab(Fragment.class, null, R.drawable.icon1, Res.S(R.string.tab1));

            // I have not implement the tab fragment yet!

            // tab two
            addTab(Fragment.class, null, R.drawable.icon2, Res.S(R.string.tab2));
        }

        private void addTab(Class<?> fragmenClass, Bundle bundle, int drawableId, String tag)
        {
            TabHost.TabSpec spec = tabHost.newTabSpec(tag);
            View tabIndicator = LayoutInflater.from(context).inflate(R.layout.item_tab, tabHost.getTabWidget(), false);
            ImageView icon = (ImageView) tabIndicator.findViewById(R.id.tab_image);
            icon.setImageResource(drawableId);
            spec.setIndicator(tabIndicator);
            tabManager.addTab(spec, fragmenClass, bundle);
        }

        @Override
        public void onSaveInstanceState(Bundle outState) 
        {
            super.onSaveInstanceState(outState);
            outState.putString(EXTRA_TAB, tabHost.getCurrentTabTag());
        }

        public static class TabManager implements TabHost.OnTabChangeListener 
        {
            private final FragmentActivity activity;
            private final TabHost tabHost;
            private final int containerID;
            private final HashMap<String, TabInfo> tabs = new HashMap<String, TabInfo>();
            TabInfo lastTab;
            TabFactory factory;

            public TabManager(FragmentActivity activity, TabHost tabHost, int containerID) 
            {
                this.activity = activity;
                this.tabHost = tabHost;
                this.containerID = containerID;
                factory = new TabFactory(activity);
                tabHost.setOnTabChangedListener(this);
            }

            public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) 
            {
                tabSpec.setContent(factory);
                String tag = tabSpec.getTag();

                TabInfo info = new TabInfo(tag, clss, args);

                info.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);

                if (info.fragment != null && !info.fragment.isDetached()) 
                {
                    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
                    ft.detach(info.fragment);
                    ft.commit();
                }

                tabs.put(tag, info);
                tabHost.addTab(tabSpec);
            }

            @Override
            public void onTabChanged(String tabId) 
            {
                TabInfo newTab = tabs.get(tabId);
                if (lastTab != newTab) 
                {
                    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
                    if (lastTab != null) {
                        if (lastTab.fragment != null) 
                        {
                            ft.detach(lastTab.fragment);
                        }
                    }
                    if (newTab != null) 
                    {
                        if (newTab.fragment == null) 
                        {
                            newTab.fragment = Fragment.instantiate(activity, newTab.clss.getName(), newTab.args);
                            ft.add(containerID, newTab.fragment, newTab.tag);
                        } 
                        else 
                        {
                            ft.attach(newTab.fragment);
                        }
                    }

                    lastTab = newTab;
                    ft.commit();
                }
            }
        }

        private static class TabInfo 
        {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;

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

        private static class TabFactory implements TabHost.TabContentFactory 
        {
            private final Context mContext;

            public TabFactory(Context context) 
            {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) 
            {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }
    }

But onCreateView method calls two or more times when device changes orientation.

My FragmentActivity is:

    public class MaptrixFragmentActivity extends FragmentActivity
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.factivity);

            replace(R.id.factivity_content, new TabsFragment(), false, false);
        }

        @Override
        protected void onResume()
        {
            super.onResume();
        }

        @Override
        protected void onPause()
        {
            super.onPause();
        }

        public void replace(int rootView, Fragment fragment)
        {
            replace(rootView, fragment, true,  true);
        }

        public void replace(int rootView, Fragment fragment, boolean backStack)
        {
            replace(rootView, fragment, backStack,  true);
        }

        public void replace(int rootView, Fragment fragment, boolean backStack,  boolean animation)
        {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (animation) transaction.setCustomAnimations(R.anim.fragment_open, R.anim.fragment_close, R.anim.fragment_open_stack, R.anim.fragment_close_stack);
            if (backStack) transaction.addToBackStack(null);
            transaction.replace(R.id.factivity_maptrix_content, fragment);
            transaction.commit();
        }
    }

Why i have this bug?

  • 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-01T15:42:03+00:00Added an answer on June 1, 2026 at 3:42 pm

    You could perhaps add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml file. This avoids recreating the activity when orientation changes and therefore the TabsFragment won’t be created twice.

    <activity android:name="MaptrixFragmentActivity" android:configChanges="keyboardHidden|orientation">
    

    See this for more details.

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

Sidebar

Related Questions

I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
I have this very simple example that I am using to learn structs in
this is a simple question. I have a form that is being validated using
i have this simple code to load data from other php page using get
Could someone help me on this, I have created simple web services using axis2
I have started using Simple-form and Bootstrap and I have tried to follow this
Using this answer , I created a sample localized app. My question is, is
I'm using this jQuery inline datePicker plugin sample but I don't know how to
I have changed the wix tutorial codes from here http://www.tramontana.co.hu/wix/ to make my own
i m using this article http://blogs.msdn.com/delay/archive/2009/09/23/if-it-walks-like-a-duck-and-talks-like-a-duck-it-must-be-a-treegrid-a-simple-xaml-only-treegrid-ui-for-wpf.aspx to have hierarchical data... i am having treeview

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.