在Tab中保存片段状态

时间:2013-05-19 11:36:41

标签: android android-fragments

我制作了一个带有两个标签的ActionBar - 一个用于碎片和bfragment。 afragment包含一个按钮。单击afragment上的按钮将使其更改为cfragment。但是,再次单击选项卡会导致再次加载碎片。我可以做些什么来防止这种情况,基本上对标签说:“当有人点击你时,加载最后一个片段状态(在这种情况下,cfragment)?”

这是我的MainActivity.java:

public class MainActivity extends Activity {

public static Context appContext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    appContext = getApplicationContext();

    //ActionBar
     ActionBar actionbar = getActionBar();
     actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

     ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
     ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");

     Fragment PlayerFragment = new AFragment();
     Fragment StationsFragment = new BFragment();

     PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
     StationsTab.setTabListener(new MyTabsListener(StationsFragment));

     actionbar.addTab(PlayerTab);
     actionbar.addTab(StationsTab);
}

我的TabListener:

class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;

public MyTabsListener(Fragment fragment) {
    this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}
}

最后,我的AFragment.java:

public class AFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.afragment, container, false);
    Button mButton = (Button) view.findViewById(R.id.button1);
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            CFragment fragment = new CFragment();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        }
    });
    return view;
}
}

如何将cfragment保存到选项卡中,以便单击第一个选项卡不会导致再次加载碎片?谢谢。

1 个答案:

答案 0 :(得分:1)

状态未保存,因为每次更改标签时都会添加新片段。

在'onTabUnselected`中,不要删除,而是分离/隐藏

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  if (fragment != null) {
    ft.detach(fragment); 
  }
}

onTabSelected中,检查片段是否已存在而不是替换

public void onTabSelected(Tab tab, FragmentTransaction ft) {
  fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
  if( fragment == null ) {
    fragment = Fragment.instantiate(mActivity, mClass.getName());
    ft.add(R.id.fragment_container, fragment, tag);
  } else {
    ft.attach(fragment);
  }
}