Search code examples
androidandroid-fragmentsfragment-tab-host

fragment Repeat in tabhost when changing the tab


I am working on FragmentTabHost with Fragment. My FragmentTabHost has 4 different fragments but when i changing tabs then after some changes 2 or 3 tabs has same fragment. Like index0 index1 index3 has SettingFragment. But they have different fragment. This is my code for setup tabs in MainActivityFragment which extends FragmentActivity

private void setTabs() {
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);



//  TabHost.TabSpec spec;
    //Intent intent;

     mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator(null, getResources().getDrawable(R.drawable.icon_alert_tab)),
                AlertFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.icon_wlmuser_tab)),
                UserFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator(null, getResources().getDrawable(R.drawable.icon_chat_tab)),
                ChatFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab4").setIndicator(null, getResources().getDrawable(R.drawable.icon_setting_tab)),
                SettingFragment.class, null);

        mTabHost.getTabWidget().setStripEnabled(false);
        mTabHost.setCurrentTab(1);
    }

my 4 fragments are AlertFragment, UserFragment, ChatFragment, SettingFragment and they has buttons. On the click on buttons another fragments open by FragmentTransaction

LIKE in UserFragment i call ContectFragment in FrameLayout by inviteBtn button

inviteBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // replace with contact fragment
            fragment = new ContactFragment();

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
            ft.replace(android.R.id.tabcontent, fragment);
            ft.addToBackStack(null);
            // Start the animated transition.
            ft.commit();
        }
    });

. The problem occur when i open inside fragments.


Solution

  • Hi I have fixed you point see below solution. Ideally your have to create one extra BaseContainerFragment. It has the framelayout. In next create the container fragment for each of your four static fragment. let's say I have two fragment with it related container fragment. see below code.

    Original Fragment :

    1. AlertFragment.java
    2. ChatFragment.java

    BaseContainerFragment.java

    public class BaseContainerFragment extends Fragment {

        public BaseContainerFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        public void replaceFragment(Fragment fragment, boolean addToBackStack) {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            if (addToBackStack) {
                transaction.addToBackStack(null);
            }
            transaction.replace(R.id.container_framelayout, fragment);
            transaction.commit();
            getChildFragmentManager().executePendingTransactions();
        }
    
        public boolean popFragment() {
            boolean isPop = false;
            if (getChildFragmentManager().getBackStackEntryCount() > 0) {
                isPop = true;
                getChildFragmentManager().popBackStack();
            }
            return isPop;
        }
    
    }
    

    Container Fragment which extends the above BaseContainerFragment:

    1. AlertContainerFragment.java extends BaseContainerFragment.java
    2. ChatContainerFragment.java extends BaseContainerFragment.java

    Now In your FragmentActivity you have to add ContainerFragment of each fragment inside FragmentTabHost.see the below code.

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTabHost;
    import android.view.Menu;
    import android.view.MenuItem;
    
    
    public class TestSukhwantTabHost extends FragmentActivity  {
    
        private FragmentTabHost mTabHost;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_sukhwant_tab_host);
            mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
    
            mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),AlertContainerFragment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),ChatContainerFragment.class, null);
            mTabHost.getTabWidget().setStripEnabled(false);
            mTabHost.setCurrentTab(0);
        }
    }
    

    Now I post the Container Fragment code, this container fragment actually load our original Fragment. To load the fragment inside the fragment we used the container fragment method to replace the new fragment.

    fragment_base_container.xml

     <?xml version="1.0" encoding="utf-8"?>
     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     </FrameLayout>
    

    AlertContainerFragment.java

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class AlertContainerFragment extends BaseContainerFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_base_container, container, false);
        }
    
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            initView();
        }
    
        private void initView() {
            replaceFragment(new AlertFragment(), false);
        }
    }
    

    ChatContainerFragment.java

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class ChatContainerFragment extends BaseContainerFragment {
    
    
        public ChatContainerFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_base_container, container, false);
        }
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            initView();
        }
    
        private void initView() {
            replaceFragment(new ChatFragment(), false);
        }
    }
    

    Original Fragment :

    Below is AlertFragment.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    
    public class AlertFragment extends Fragment {
    
        private Button framgnet1_button;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View mView = inflater.inflate(R.layout.fragment_alert, container, false);
            framgnet1_button = (Button) mView.findViewById(R.id.framgnet1_button);
            framgnet1_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((BaseContainerFragment)getParentFragment()).replaceFragment(new DynamicAlertFramgnet(), false);
                }
            });
            return mView;
        }
    }
    

    ChatFragment.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    
    public class ChatFragment extends Fragment {
    
        private Button button_chart_fragment;
    
        public ChatFragment() {
            // Required empty public constructor
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View mView = inflater.inflate(R.layout.fragment_chat, container, false);
            button_chart_fragment = (Button)mView.findViewById(R.id.button_chart_fragment);
            button_chart_fragment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((BaseContainerFragment)getParentFragment()).replaceFragment(new DynamicChartFramgnet(), false);
                }
            });
            return mView;
        }
    }
    

    In above code what i had implemented, each original fragment has button when you click on it, it will load the new fragment.

    Let me know if you have any question & pleas give your suggestion.

    Thank you