如何在父片段恢复

时间:2016-09-14 17:30:30

标签: android android-fragments

我有一个名为MainFragment的片段TabLayoutTabLayout有两个标签,每个标签都有一个片段。让我们称之为Fragment AFragment B

当我打开MainFragment时,Fragment AFragment B按预期填充在TabLayout内。当我恢复MainFragment时,Fragment AFragment B未恢复,我会收到两个空标签。

我的MainFragment看起来像这样

    @Override
    public View onCreateView(
            LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_fragment, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Injector.component().inject(this);

        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
        adapter.addFragment(new FragmentA(), "Fragment A");
        adapter.addFragment(new FragmentB(), "Fragment B");
        viewPager.setAdapter(adapter);
    }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

碎片B& MainFragmentgetChildFragmentManager()的子片段,使用getFragmentManager()代替// JavaScript Document var xmlhttp; function generaGrafico(nome){ xmlhttp = GetXmlHttpObject(); if (xmlhttp == null){ alert ("Browser does not support HTTP Request"); return; } var url = "php/creaGrafico.php"; url = url+"?nome="+nome; xmlhttp.onreadystatechange = stateChanged; xmlhttp.open("GET", url, true); xmlhttp.send(); } function stateChanged(){ if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ var spanstat = document.getElementById("stats"); spanstat.innerHTML = xmlhttp.responseText; } } function GetXmlHttpObject(){ if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject){ // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }