据我了解,每个Fragment都有其自己的后台堆栈,这与属于FragmentActivity的所有 fragment 共享。假设您必须管理多个选项卡,并且每个选项卡都可以浏览多个 fragment 。假设您要“记录”每个选项卡的导航历史记录,因此在 fragment 之间进行切换将使您可以返回到正在查看的 fragment 。有可能实现吗?我是否需要将每个选项卡链接到 fragment Activity ?在这种情况下,如何管理FragmentActivity之间的切换?

最佳答案

由于不建议采用这种设计风格,因此没有“标准”的解决方法。但是,我找到了一种使其工作的方法:您将手动设计导航。

您的应用程序应该有一个Activity,一个FragmentActivity。它具有一个FragmentTabHost,它将容纳您的每个TabFragments。

TabFragment是我创建的抽象类,用于表示TabSpec中的选项卡。它将在选项卡中管理 fragment 的导航和交换。

然后,可以在TabFragment对象内交换您创建的各个Fragment。这是代码:

Activity

    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实例的内部应该是一个可将子Fragment附加到其上的FrameLayout。第一次单击选项卡时,它将启动onCreate()中指定的 fragment 。从另一个选项卡切换回它之后,它将恢复上一次显示的任何 fragment 。如果需要分层导航,则应使用onBackPressed()方法在 fragment 中向后导航。我使用了byte属性(tabContentIndex)来确定如何导航。如果您添加采用此TabFragment的实例的构造函数,则Fragment可以将其自身交换为其他Fragment。他们将通过访问start(Example)Fragment()方法来做到这一点。请记住,“返回”按钮必须最终退出应用程序。
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();
        }
    }
}

关于android - 具有多个后堆栈的 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9598475/

10-10 01:54