Question

I have 2 tabs in my app.Each have single fragment initially.

Scenario 1(Tab 1): I 'm adding a second fragment in the tab on Button Click from the first fragment & again a third fragment on button click from second fragment.

Scenario 2(Tab 2): Similar as tab1

Scenario 2(Switch between the Tab): As I switch between the tab.I'm unable to get same tab fragment which I have added in the respective tab.

Below is my code:(First Fragment Tab1)

    public class ArticleFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            ViewGroup view=(ViewGroup) inflater.inflate(R.layout.article_fragment, container,false);
            fragmentTabHost.setup(getActivity(), getChildFragmentManager() , R.id.frag_container);

            Button article_button = (Button) view.findViewById(R.id.article_btn);

            article_button.setOnClickListener( new OnClickListener() {

                @Override
                public void onClick(View v) {

                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    article_txt.setText("Changed Value");

                    ArticleDetails  articleFragment = new ArticleDetails();
                    fragmentTransaction.add(ScreenFragmentActivity.getContainertId(), articleFragment, AppConstants.ARTICLE_FRAGMENT_DETAILS);
                    fragmentTransaction.hide(ArticleFragment.this);
                    fragmentTransaction.addToBackStack(ArticleFragment.class.getName());

                    fragmentTransaction.commit();

                }
            });
        return view;
    }
}

Second Fragment Tab1:

public class ArticleDetails extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,

        ViewGroup view=(ViewGroup) inflater.inflate(R.layout.article_details, container,false);
        Button article_button = (Button) view.findViewById(R.id.article_details_btn);

        article_button.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {

                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                ArticleDetailsWithIn  articleFragment = new ArticleDetailsWithIn();
                fragmentTransaction.add(ScreenFragmentActivity.getContainertId(), articleFragment,
AppConstants.ARTICLE_FRAGMENT_DETAILS_WITHIN);
                fragmentTransaction.hide(ArticleDetails.this);
                fragmentTransaction.addToBackStack(null);

                fragmentTransaction.commit();

            }
        });

        return view;
    }

My Layout:

 <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@+id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

        </LinearLayout>
 </android.support.v4.app.FragmentTabHost>

So Basically I'm trying to add the new Fragment within the tab .But on Switch I'm not able to retain my respective fragment

Was it helpful?

Solution

You can try this - tag your fragments with some name and store this in any static helper class using getter/setter method. Make your first fragment tag name as default (this is for first time launch). Then at place where you have logic to add new fragments, put the required if-else statements. Same goes for other tab also. (you can even use shared preferences, if you want to remember last seen fragment even when app is closed and restarted again.)

Create a static helper class.

public class HelperClass {
    private static string TabOneFragmentOnScreen = "";

    public static String getTabOneFragmentOnScreen () {
    return TabOneFragmentOnScreen ;
}
public static void setTabOneFragmentOnScreen(
        String TabOneFragmentOnScreen) {
    HelperClass.TabOneFragmentOnScreen = TabOneFragmentOnScreen;
}
}

When you click one TabOne

if(HelperClass.getTabOneFragmentOnScreen.equals("")){
    HelperClass.setTabOneFragmentOnScreen("FragmentOne");
    //logic to add fragment one goes here
}

if(HelperClass.getTabOneFragmentOnScreen.equals("FragmentTwo")){
    HelperClass.setTabOneFragmentOnScreen("FragmentTwo");
    //logic to add fragment two goes here
}

This way it will always remember the last fragment on screen under tab one.

This code is not complete, but you can develop further on this logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top