Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fragment handling screen orientation with tabs in actionbar

Can someone explain to me or maybe point a link or an example of how one should handle screen orientation when it comes to fragments. The problem that I am having at the moment is that I have an app with an action bar with three tabs. Each tab containing one fragment. So when i flip the device, only the one fragment that I set as selected using

actionbar.setSelectedNavigationItem(1) 

is shown (for no confusion, the other tabs are visible, but when I click on them nothing happens, they can't be selected and shown. Their content is never shown). Is there something that is done in these cases? Can someone give me an advice, I would appreciate it. Thank u.

like image 841
Sandra Avatar asked Mar 22 '12 09:03

Sandra


1 Answers

I had the same problem and found a solution here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.html

Problem is that there are already fragments for the tabs from the previously saved state. You have to deactivate them.

Here's the code-snippet for the constructor of your TabListener:

public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
            ft.detach(mFragment);
            ft.commit();
        }
    }

Hope I could help you.

like image 198
Oli Avatar answered Nov 12 '22 01:11

Oli