Switching between fragmentTabs giving unexpected results
Asked Answered
E

2

5

I am trying to implement FragmentTabs as illiustrated in http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.html. Everything went well until i did this:

I started lots of different fragments from one tab like:

tab1-->fragment1--->fragment2--->fragment3

tab2

But when i switched to tab2 and again came back to tab1, I got fragment1 screen not fragment3(i.e. I have 3 fragments in first tab and while i am on 3rd fragment and I come again on first tab after switching to second tab, I am taken to 1st fragment not 3rd)..Can anyone tell what might be the prob?

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

                mLastTab = newTab;
                ft.commit();
                mActivity.getSupportFragmentManager().executePendingTransactions();
            }
        }

When i comment attach() and detatch(), I get this :

enter image description here

Enphytotic answered 15/5, 2012 at 12:28 Comment(7)
You don't have much information here to let us find the problem. If anything, paste the code where you are are setting up the TabHost/TabManager and adding the tabs to it as shown in the example.Oas
i am refering the link that i've given in the question.Enphytotic
I've personally used that example source before, and it is fully working. I'm asking for your use of it as it is likely an issue in your implementation.Oas
If you have used this example then you must be knowing that we have to give fragment class's name where we add the tab.I am also doing that..Now from that fragment class i am starting a new fragment..Now the issue is that when i am on let's say 3rd fragment and i switch to other tabs and then again come to first tab..I am being forwarded to fragment1 Screen which should not happen, it should stay on present tab only.. I think onTabChanged() must be modified..Can u tell how can i remain on sameFragment from where i switched to second tab..?Enphytotic
The problem area is your onTabChanged(String tabId) override. Add this code to your post.Kreisler
Can you tell me what to change..I am pissed off seirously by this thingEnphytotic
I think commenting the attach and detach calls will retain the fragments in the tabs...Kreisler
P
2

Without more details it's tough to say what's wrong specificaly. However, I can say from personal experience that when I first had to implement fragment tabs, I went through a lot of lousy tutorials before finding something that worked. The tutorial that finally made sense for me is here: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

As a bonus, there's also some Github source code: https://github.com/mitchwongho/Andy/tree/master/Andy/src/com/andy/fragments/tabs

Puzzle answered 15/5, 2012 at 17:9 Comment(1)
Hey the tutorial helped me a lot but i am looking for something else.In the tutorial you suggeted, there is only one fragment per tab. In my case I have 3 fragments in first tab and while i am on 3rd fragment and I come again on first tab after switching to different tabs, I am taken to 1st fragment not 3rd. Hope i'm not confusing you.Enphytotic
K
2

Update:

I recommend you retain the original code for onTabChanged and use addToBackStack method for maintaining the traversal state of fragments. Call addToBackStack when going from one fragment to next, like when adding or replaceing fragments.

Also change the TabInfo.fragment reference to reflect the transitions between fragments inside a tab.


Do not attach and detach every time tab is changed.

   @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);
        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.replace(mContainerId, newTab.fragment, newTab.tag);
                } 
            }
            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }
Kreisler answered 19/5, 2012 at 5:4 Comment(7)
By commenting ..Content of both the tabs are mixing into one screenEnphytotic
What do you mean mixing into 1 screen? howKreisler
identifier of the container this fragment is to be placed in.Enphytotic
I updated the question , see both the tabs are merged into one.Enphytotic
Where do you call addToBackStack?Kreisler
can u give me ur id? I will mail u whole project..?Enphytotic
join FfragmentsTab room in chat.. chat.stackoverflow.com/rooms/11650/ffragmentstabKreisler

© 2022 - 2024 — McMap. All rights reserved.