Android 多重反褶破片

Android 多重反褶破片,android,fragment,Android,Fragment,据我所知,每个片段都有自己的backbackback,这与属于FragmentActivity的所有片段共享。假设您必须管理多个选项卡,每个选项卡都可以在多个片段中导航。假设您希望“记录”每个选项卡的导航历史,那么在片段之间切换将允许您返回到您正在查看的片段。有可能实现吗?我是否需要将每个选项卡链接到片段活动?在这种情况下,如何管理碎片活动之间的切换 这不是一个好的导航方法。我推荐你。看看Android的设计模式,重新思考你的流程 但是,如果不编写自己的FragmentManager,您需要有多

据我所知,每个片段都有自己的backbackback,这与属于FragmentActivity的所有片段共享。假设您必须管理多个选项卡,每个选项卡都可以在多个片段中导航。假设您希望“记录”每个选项卡的导航历史,那么在片段之间切换将允许您返回到您正在查看的片段。有可能实现吗?我是否需要将每个选项卡链接到片段活动?在这种情况下,如何管理碎片活动之间的切换

这不是一个好的导航方法。我推荐你。看看Android的设计模式,重新思考你的流程

但是,如果不编写自己的
FragmentManager
,您需要有多个
FragmentActivities

看看如何使用,但这只会在蜂巢上真正起作用。因此,我建议您重新考虑导航,或者只构建一个HC+应用程序

我知道我没有给你太多的代码来真正解决你的问题。但是请仔细阅读它,它解释了时间和祖先导航,这是从Android 3.0+开始“正式”添加的。

没有“标准”的方式来实现这一点,因为这种设计风格是不受鼓励的。然而,我找到了一种方法让它工作:你将手动设计你的导航

您的应用程序应该有一个活动,一个片段活动。它有一个FragmentTabHost,用于保存每个TabFragments

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

然后,您创建的各个片段可以在TabFragment对象中交换。代码如下:

活动

    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,子片段可以附加到该FrameLayout。第一次单击选项卡时,它将启动
onCreate()
中指定的片段。在从另一个选项卡切换回它之后,它将恢复上次显示的任何片段。如果需要分层导航,则应使用onBackPressed()方法在片段中导航。我使用字节属性(
tabContentIndex
)来确定如何导航。如果您添加一个构造函数来获取这个TabFragment的实例,那么这些片段可以将自己替换为其他片段。他们将通过访问
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();
        }
    }
}

FragmentTransaction有一个方法

打开新的片段方式:

使用这种方法,当用户按下后退按钮时,它显示当前选项卡的上一个片段,当片段堆栈清空当前选项卡时,它退出应用程序


addToBackStack()方法将此事务添加到后堆栈。这意味着事务在提交后将被记住,并在稍后从堆栈弹出时反转其操作。

是否有解决方法?我需要在一个iOS应用程序上制作一个Android应用程序,因此需要它来导航相同的应用程序。老兄,这看起来像是谷歌正式给出了这个答案,我们现在在Android中有了导航组件,我认为它遵循相同的方法
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();