选择ActionbarSherlock选项卡时会重新创建片段

时间:2013-04-18 06:17:04

标签: android android-actionbar actionbarsherlock android-tabs

我有一个片段活动,它包含三个片段。

public class MainActivity extends SherlockFragmentActivity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = bar.newTab();
    ActionBar.Tab tab2 = bar.newTab();
    ActionBar.Tab tab3 = bar.newTab();
    tab1.setText("");
    tab1.setIcon(R.drawable.abs__ic_menu_share_holo_dark);
    tab2.setText("");
    tab2.setIcon(R.drawable.abs__ic_voice_search);
    tab3.setText("");
    tab3.setIcon(R.drawable.abs__ic_cab_done_holo_dark);

    if (savedInstanceState == null) {

        tab1.setTabListener(new MyTabListener());
        tab2.setTabListener(new MyTabListener());
        tab3.setTabListener(new MyTabListener());
        bar.addTab(tab1);
        bar.addTab(tab2);
        bar.addTab(tab3);

    }
}

private class MyTabListener implements ActionBar.TabListener {
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        switch (tab.getPosition()) {
        case 0:

            FeedsActivity frag = new FeedsActivity();

            ft.replace(android.R.id.content, frag);
            return;

        case 1:
            ProfileActivity frag2 = new ProfileActivity();
            ft.replace(android.R.id.content, frag2);
            return;
        case 2:
            MyMemoirsActivity frag3 = new MyMemoirsActivity();
            ft.replace(android.R.id.content, frag3);
            return;

        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
}

}

这是第一个片段,

public class FeedsActivity extends SherlockFragment {

public static String[] MainCategory; 
public static String[] MainCategoryId; 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group,
        Bundle saved) {
    setRetainInstance(true);
    return inflater.inflate(R.layout.activity_feeds, group, false);
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);



         new GetMainCategory(getActivity()).execute();




}
}

当我选择第二个选项卡然后选择第一个选项卡时,将再次调用第一个片段中的异步任务。如何保留第一个片段的状态以使其视图创建一次?我使用了setRetainInstance(true),但没有用。

2 个答案:

答案 0 :(得分:2)

我通过使用show and hide而不是attach and detach解决了这个问题。

   private class MyTabListener implements ActionBar.TabListener {
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            switch (tab.getPosition()) {
            case 0:

                if (frag1 == null) {
                    // If not, instantiate and add it to the activity
                    frag1 = Fragment.instantiate(getApplicationContext(),
                            FeedsActivity.class.getName());
                    ft.add(android.R.id.content, frag1, "Feeds");
                } else {
                    // If it exists, simply attach it in order to show it
                    ft.show(frag1);
                }
                return;

            case 1:
                if (frag2 == null) {
                    // If not, instantiate and add it to the activity
                    frag2 = Fragment.instantiate(getApplicationContext(),
                            ProfileActivity.class.getName());
                    ft.add(android.R.id.content, frag2, "Profile");
                } else {
                    // If it exists, simply attach it in order to show it
                    ft.show(frag2);
                }
                return;
            case 2:

                if (frag3 == null) {
                    // If not, instantiate and add it to the activity
                    frag3 = Fragment.instantiate(getApplicationContext(),
                            History.class.getName());
                    ft.add(android.R.id.content, frag3, "History");
                } else {
                    // If it exists, simply attach it in order to show it
                    ft.show(frag3);
                }

                return;

            }

        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            if (frag1 != null) {
                // Detach the fragment, because another one is being attached
                switch (tab.getPosition()) {
                case 0:
                    ft.hide(frag1);
                    return;
                case 1:
                    ft.hide(frag2);
                    return;
                case 2:
                    ft.hide(frag3);
                    return;

                }

            }
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    }

答案 1 :(得分:1)

使用TabListener的正确方法是:

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // Check if the fragment is already initialized
    if (mFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if (mFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.detach(mFragment);
    }
}

@override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // User selected the already selected tab. Usually do nothing.
}

基本上你需要检查这是否是第一次初始化Fragment。如果没有,您应该将其添加到屏幕上,当用户取消选择选项卡时,您应该分离当前可见的片段并添加新片段。这是应该的方式。每次用户单击选项卡时,您都不需要创建新的Fragment实例。