碎片与多个backstack [英] Fragment with multiple backstack

查看:115
本文介绍了碎片与多个backstack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,每个片段都有自己的backstack,这是与所有属于FragmentActivity片段共享。假设你有管理多个标签,每个标签可以浏览多个片段。假设你想记录导航历史的每一个标签这样的碎片可以让你回到你查看的片段之间的切换。是否有可能实现?我需要每一个标签链接到一个片段活动?在这种情况下,怎么能FragmentActivity之间的切换管理的?

As far as I understand, every Fragment has its own backstack, and this is shared with all the fragments that belongs to the FragmentActivity. Suppose you have to manage multiple tabs, and every tab could navigate through multiple fragment. Suppose you want to "record" the navigation history for every tab so switching between fragments will allow you to return to the fragment you were viewing. Is it possible to achieve? Do I need to link every tab to an fragment activity? In this case how can be the switching between FragmentActivity managed?

推荐答案

还有就是要对这个,因为这种设计风格是不鼓励没有标准的方式。然而,我发现了一种方法,使其工作:你需要手工设计导航

There is no "standard" way of going about this, because this design style is discouraged. However, I found a way to make it work: You will design your navigation manually.

您的应用程序应该有一个活动,一个FragmentActivity。它有一个FragmentTabHost,将举行您的每一个TabFragments的。

Your application should have one Activity, a FragmentActivity. It has a FragmentTabHost that will hold each of your TabFragments.

TabFragment是我创建重新present您的选项卡中则tabspec抽象类。它要管理的导航和片段的交换中的选项卡。

TabFragment is the abstract class I created to represent your tab in the TabSpec. It will manage the navigation and swapping of fragments WITHIN the tab.

然后,创建单独的片段可以在TabFragment对象中交换。这里是code:

Then, the individual Fragments you create can be swapped within the TabFragment object. Here is the code:

的活动

    public class MainActivity extends FragmentActivity {

            private FragmentTabHost tabHost;

//(TabFragment)s will set this property when created so the Activity can communicate with it
            public TabFragment activeFragment;

            @Override
            public void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

        //create tabHost based on .xml layout
                tabHost = (FragmentTabHost)findViewById(R.id.tabhost);
                tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

        //add each of your tabs to the tabHost. These are all (TabFragment)s
                tabHost.addTab(tabHost.newTabSpec("New Tab").setIndicator("New Tab"),
                      ExampleTabFragment.class, null);
            }

    /*override the onBackPressed method, so that your application does not close every 
time the user navigates back. Instead, calls the activeFragment to determine behavior*/
            @Override
            public void onBackPressed() {
                activeFragment.onBackPressed();
            }

    //method for TabFragment to call when the user navigates out of the app
            public void close(){
                super.onBackPressed();
            }
        }

TabFragment

    public abstract class TabFragment extends Fragment {

        @Override
        public void onResume(){

//sets the property in the Activity so we can reference this from it.
            ((MainActivity) getActivity()).activeFragment=this;
            super.onResume();
        }

//this will be the method called when the back button is pressed. It will navigate.
        public abstract void onBackPressed();

    }

实例TabFragment的 里面TabFragment的实例应该是一个的FrameLayout到子片段可以连接。第一次的标签被点击时,它会推出的onCreate指定的片段()。切换回从另一个选项卡后,将恢复的任何片段,最后显示。所述onBack pressed()方法应用于导航返回通过片段如果分层导航是期望的。我用了一个字节的属性( tabContentIndex )来决定如何操作。碎片可以换自己的其他片段,如果你添加一个构造函数,这个TabFragment和实例。他们将通过访问启动(例)片段()的方法做到这一点。请记住,返回按钮,最终要退出的应用程序。

Instance of TabFragment Inside an instance of a TabFragment should be a FrameLayout to which child Fragments can be attached. The first time the tab is clicked, it will launch the Fragment specified in onCreate(). After switching back to it from another tab, it will resume whatever Fragment was last displayed. The onBackPressed() method should be used to navigate back through the fragments if hierarchical navigation is desired. I used a byte property (tabContentIndex) to determine how to navigate. The Fragments can swap themselves for other Fragments if you add a constructor that takes and instance of this TabFragment. They will do this by accessing the start(Example)Fragment() methods. Remember that the 'back' button must eventually exit the app.

public class NewTrailTabContent extends TabFragment {

    //to determine what child Fragment is active
    byte tabContentIndex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

//a simple FrameLayout in this case. Child Fragments will be attached.
        return inflater.inflate(R.layout.example_fragment, container,
                false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

//The tab starts with this Fragment
        startDiologFragment();
        super.onCreate(savedInstanceState);
    }

    public void startExampleFragment(){

/*Fragment's constructor allows us to reference the parent to navigate. In effect, this 
Fragment will be able to call these navigation methods.*/
        ExampleFragment newFragment = new ExampleFragment(this);
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

//this Resource is the FrameLayout
        fragmentTransaction.replace(R.id.example_contentpane,
                newFragment);
        fragmentTransaction.commit();

//this is set so the onBackPressed() method knows how to operate.
        tabContentIndex =0;
    }

    public void startTestFragment(){

        Fragment testFragment = new TestFragment(this);
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.replace(R.id.example_contentpane,
                testFragment);
        fragmentTransaction.commit();

//different contentIndex
        tabContentIndex = 1;
    }

//this method called by the Activity
    @Override 
    public void onBackPressed() {

//this will close the app because we are at the top of the hierarchy
        if(tabContentIndex==0){
            ((MainActivity)getActivity()).close();

//this will switch to the original content fragment.
        }else if(tabContentIndex==1||tabContentIndex==2){
            startExampleFragment();
        }
    }
}

这篇关于碎片与多个backstack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆