简体   繁体   中英

How do I save the state of tabs?

In my app, you use the interface in each tab. But when you change tabs, then return it does not save. How do I implement this?

Main Activity

 ActionBar.Tab tab1, tab2;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_screen);

    ActionBar actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);


    tab1 = actionBar.newTab().setText("Skate Dice");
    tab2 = actionBar.newTab().setText("S.K.A.T.E");

    tab1.setTabListener(new MyTabListener(fragmentTab1));
    tab2.setTabListener(new MyTabListener(fragmentTab2));

    actionBar.addTab(tab1);
    actionBar.addTab(tab2);

Tab Listener

public class MyTabListener implements ActionBar.TabListener
{
Fragment fragment;

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

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

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

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
    // nothing done here
}

}

How do I save the state of my tabs when, the user unselects then comes back to the tab? I am new to Android Development any advice/help will be much appreciated thank you!

Look at this answer and also check out this project

Those will hopefully set you on the right direction. Another helpful item to look into is managing the fragment life cycle . This can give you the ability to do something with the data before the fragment is paused or destroyed (such as save it).

EDIT: http://developer.android.com/reference/android/app/ActionBar.TabListener.html gave some good information. Instead of removing your fragment completely, use the attach() and detach() methods. Something like:

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (fragment == null) {
            ft.replace(R.id.fragment_container, fragment); // or use ft.add()
        } else {
            ft.attach(fragment);
        }
    }

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

I haven't been able to test it yet but it should be along those lines... Sorry for another link, but this is helpful too.

最简单的答案-每个选项卡都需要有自己的TabListener,因此每个选项卡的片段都保存在TabListener实现中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM