Adam
Adam

Reputation: 2570

Blank fragments on application restart

Whenever I open my application for the first time, there are three fragments displayed correctly in 3 containers I have in my xml file. Upon clicking a tab on the action bar, one of the containers' fragment is swapped for another.

The Problem

Whenever I open the app, swap to the second tab, use the back arrow to exit the app, then reopen the app and re-select the second tab, the fragment is blank. Nothing appears, and the same problem occurs with the third tab also. Why aren't the second and third fragments being displayed?

Upvotes: 1

Views: 1078

Answers (2)

ashokgelal
ashokgelal

Reputation: 81528

// from onCreate() method of your DefaultActivity class, call this method:

// file: DefaultActivity.java

...
...
...
    private void addTabs() {
        // get support ActionBar and set navigation mode to Tabs
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Add first tab to ActionBar, and set the TabListener to a new TabListner object
        String title1 = "first_tab";
        ActionBar.Tab tab1 = bar.newTab();
        tab1.setText(title1);
        tab1.setTabListener(new TabListener(this, title1, Fragment1.class));
        bar.addTab(tab1);

        // Add second tab to ActionBar, and set the TabListener to a new TabListner object
        String title2 = "second_tab";
        ActionBar.Tab tab2 = bar.newTab();
        tab2.setText(locationsTitle);
        tab2.setTabListener(new TabListener(this, tab2, Fragment2.class));
        bar.addTab(tab2);

        // Add third tab to ActionBar, and set the TabListener to a new TabListner object
        String title3 = "third_tab";
        ActionBar.Tab tab3 = bar.newTab();
        tab3.setText(title3);
        tab3.setTabListener(new TabListener(this, title3, Fragment3.class));
        bar.addTab(tab3);
    }
... 
...
...

Now, you need to crate three fragments - Fragment1, Fragment2, and Fragment3 as well as create a TabListener:

// file: TabListener.java

public class TabListener implements ActionBar.TabListener {

    private final FragmentActivity mActivity;
    private final String mTag;
    private final Class mFragmentClass;
    private Fragment mFragment;

    public TabListener(FragmentActivity activity, String tag, Class fragmentClass) {
        mActivity = activity;
        mTag = tag;
        mFragmentClass = fragmentClass;
        // see if we already have the fragment with given tag. it's okay if it is null
        mFragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            // instantiate a new fragment for the given class
            mFragment = SherlockFragment.instantiate(mActivity, mFragmentClass.getName());
            // place in the default root viewgroup - android.R.id.content
            ft.replace(android.R.id.content, mFragment, mTag);
        } else {
            if (mFragment.isDetached())
                ft.attach(mFragment);
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.detach(mFragment);
        }
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }
}

That's all you need.

Upvotes: 1

Adam
Adam

Reputation: 2570

I solved it by using a public static boolean started value in each of my fragments. In each fragment's class, in the onCreateView() I set started = true; and in the onDestroyView() I set started = false;

In the onTabSelected() method of my main activity, I read this boolean value, and if it is true, I attach the fragment back to its container, but if it is false, I create a new instance of the particular fragment and add it to the container.

Upvotes: 0

Related Questions