简体   繁体   English

如何保存标签状态?

[英]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! 我是Android开发的新手,非常感谢您的任何建议/帮助!

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. 编辑: http : //developer.android.com/reference/android/app/ActionBar.TabListener.html提供了一些很好的信息。 Instead of removing your fragment completely, use the attach() and detach() methods. 而不是完全删除您的片段,请使用attach()和detach()方法。 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实现中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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