1

アプリケーションを初めて開くと、xml ファイルにある 3 つのコンテナに 3 つのフラグメントが正しく表示されます。アクション バーのタブをクリックすると、コンテナのフラグメントの 1 つが別のフラグメントに交換されます。

問題

アプリを開いて 2 番目のタブに切り替え、戻る矢印を使用してアプリを終了し、アプリを再度開いて 2 番目のタブを再度選択すると、フラグメントは空白になります。何も表示されず、3 番目のタブでも同じ問題が発生します。2 番目と 3 番目のフラグメントが表示されないのはなぜですか?

4

2 に答える 2

1
// 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);
    }
... 
...
...

次に、Fragment1、Fragment2、および Fragment3 の 3 つのフラグメントを作成し、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) {
    }
}

それだけです。

于 2013-01-03T03:54:11.440 に答える
0

public static boolean started各フラグメントの値を使用して解決しました。各フラグメントのクラス、onCreateView()Iセットstarted = true;、およびonDestroyView()Iセットメインアクティビティstarted = false;

のメソッドでこの値を読み取り、そうである場合はフラグメントをコンテナにアタッチし直しますが、そうである場合は、新しい値を作成します。特定のフラグメントのインスタンスを作成し、コンテナーに追加します。onTabSelected()booleantruefalse

于 2013-01-03T01:55:21.557 に答える