方向变化时片段消失

时间:2014-10-14 14:57:11

标签: android android-fragments fragmentmanager

我有一个viewpager,它托管一个片段,该片段依次托管两个嵌套片段。它们被初始化很好但是当我旋转屏幕时,最后一个嵌套片段消失了(Page2Fragment)。当我再次向后旋转它时。

我认为这可能与此问题有关:

https://code.google.com/p/android/issues/detail?id=52112

但据说这个问题已得到修复,但对我来说并不适用。我的问题是,是否有人有解决方法?我发布了我在下面使用的代码:

ViewPagerActivity:

package com.example.swekjaks.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;


public class ViewPagerActivity extends FragmentActivity {

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.task_pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new MainContainerFragment();
        }

        @Override
        public int getCount() {
            return 1;
        }
    }

}

MainContainerFragment:

package com.example.swekjaks.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainContainerFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_main_container, container, false);

        FragmentManager fragmentManager = getChildFragmentManager();
        AddFirstFragment(fragmentManager);
        AddNextFragment(fragmentManager);

        return rootView;
    }

    private void AddFirstFragment(FragmentManager fragmentManager) {
        Page1Fragment fragment = (Page1Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment1);

        if (fragment == null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Page1Fragment page1Fragment = new Page1Fragment();
            fragmentTransaction.add(R.id.frameLayout_fragment1, page1Fragment);
            fragmentTransaction.commit();

        } else {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frameLayout_fragment1, fragment);
            fragmentTransaction.commit();
        }
    }

    private void AddNextFragment(FragmentManager fragmentManager) {
        Page2Fragment fragment = (Page2Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment2);

        if (fragment == null) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Page2Fragment page2Fragment = new Page2Fragment();
            fragmentTransaction.add(R.id.frameLayout_fragment2, page2Fragment);
            fragmentTransaction.commit();

        } else {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frameLayout_fragment2, fragment);
            fragmentTransaction.commit();
        }
    }
}

FragmentMainContainer的视图:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
        <FrameLayout
            android:orientation="vertical"
            android:id="@+id/frameLayout_fragment1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </FrameLayout>

        <FrameLayout
            android:orientation="vertical"
            android:id="@+id/frameLayout_fragment2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </FrameLayout>

</LinearLayout>

Page1Fragment:

package com.example.swekjaks.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Page1Fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_page_1, container, false);

        return rootView;
    }


}

Page2Fragment:

package com.example.swekjaks.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Page2Fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_page_2, container, false);

        return rootView;
    }


}

1 个答案:

答案 0 :(得分:3)

我最后通过添加以下代码解决了这个问题:

@Override
    public void onSaveInstanceState(Bundle outState) {

        //Remove fragments:
        FragmentManager fragmentManager = getChildFragmentManager();
        Page1Fragment fragment1 = (Page1Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment1);
        Page2Fragment fragment2 = (Page2Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment2);

        if (fragment1 != null){
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.detach(fragment1);
            fragmentTransaction.commit();
        }

        if (fragment2 != null){
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.detach(fragment2);
            fragmentTransaction.commit();
        }

        super.onSaveInstanceState(outState);


    }

所以,我分离片段而不是调用fragmentTransaction.replace(...)我调用

fragmentTransaction.attach(fragment);

这似乎有效,到目前为止我还没有看到任何缺点