子片段自动销毁

时间:2016-06-09 05:55:58

标签: android android-fragments fragment android-nested-fragment

我有一个父片段,即FragmentA,并在其上展开子片段FragmentB。在FragmentB的onCreateView函数试图膨胀另一个片段`FragmentC'。

FragmentA.java

public class FragmentA extends Fragment {

    private static final String TAG = "FragmentA";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {
    /* Inflate the layout for this fragment */
    View view = inflater.inflate(R.layout.root_fragment, container, false);


    FragmentManager manager = getChildFragmentManager();

    FragmentTransaction transaction = manager
        .beginTransaction();
    /*
     * When this container fragment is created, we fill it with our first
         * "real" fragment
         */
    transaction.replace(R.id.container_main, new FragmentRecharge());
    transaction.addToBackStack(null);
    transaction.commit();


    return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
    super.onSaveInstanceState(outState);
    }
}

FragmentB.java

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

    rootView = inflater.inflate(R.layout.recharge_layout_new, container,

    false);
....

try {
    ((ActivityMain) context).bannerView.setVisibility(View.GONE);
    Fragment fragment = new FragmentC();
//                fragment.setArguments(bundle);
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.container_main, fragment);
    fragmentTransaction.addToBackStack("my_fragment");
    fragmentTransaction.commit();
} catch (Exception e) {
    Crashlytics.logException(e);
}

 }  

FragmentC.java

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

    rootView = inflater.inflate(R.layout.recharge_layout_new, container,

    false);
    ....

布局文件:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fragment_back_color">

     <FrameLayout
    android:id="@+id/container_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

    </FrameLayout>

but it get destroyed automatically and come to `FragmentB`.  How can I fix this issue ?

我正在尝试将HandlerpostDelayed一起使用,间隔为1秒,然后它会膨胀FragmentC&amp;它工作正常。这看起来不是解决方案。

        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    ((ActivityMain) context).bannerView.setVisibility(View.GONE);
                    Fragment fragment = new FragmentC();
//                fragment.setArguments(bundle);
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.container_main, fragment);
                    fragmentTransaction.addToBackStack("my_fragment");
                    fragmentTransaction.commit();
                } catch (Exception e) {
                    Crashlytics.logException(e);
                }
            }
        }, 1000);

4 个答案:

答案 0 :(得分:0)

<强>更新 我在mi Github Account

中使用手动嵌套片段添加来实施测试

使用当前版本的Android支持,您可以通过getChildFragmentManager()嵌套片段。 childFragmentManager背后的基础是它推迟加载,直到前一个片段事务完成。还有另外一种方法吗?

当然!没有ChildManager的嵌套,但如何?好吧,见下文; 我们将有一个延迟片段,延迟加载,因此可以在其中加载其他片段。

这个想法很简单。但是,Handler是在当前片段事务完成提交后,找到一个非常方便的类,它有效地等待在主线程上执行空间(片段受到UI干扰,因此,它们在主线程上运行)。 p>

private final Handler handler = new Handler();
private Runnable runPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    return inflater.inflate(R.layout.recharge_layout_new, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    runPager = new Runnable() {

        @Override
        public void run()
        {
          getFragmentManager().beginTransaction().addFragment(R.id.frag_container, FragmentC.newInstance()).commit();
        }
    };
    handler.post(runPager);
}

/**
 * @see android.support.v4.app.Fragment#onPause()
 */
@Override
public void onPause()
{
    super.onPause();
    handler.removeCallbacks(runPager);
}

答案 1 :(得分:0)

我按照自己的方式做到了它并没有破坏片段C它的工作正常。

TestFragmentActivity.java

public class TestFragmentActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_test);

        FragmentA fragmentA = new FragmentA();
        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        mTransaction.replace(R.id.container_main, fragmentA).commit();
    }

}

FragmentA.java

public class FragmentA extends Fragment
{
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return view = inflater.inflate(R.layout.fragment_a,container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        FragmentB fragmentB = new FragmentB();
        FragmentTransaction mTransaction = getChildFragmentManager().beginTransaction();
        mTransaction.replace(R.id.xLinLayFragmentContentB, fragmentB).commit();

    }
}

FragmentB.java

public class FragmentB extends Fragment
{
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return view = inflater.inflate(R.layout.fragment_b,container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        FragmentC fragmentC = new FragmentC();
        FragmentTransaction mTransaction = getChildFragmentManager().beginTransaction();
        mTransaction.replace(R.id.xLinLayFragmentContentC, fragmentC).commit();
    }
}

FragmentC.java

public class FragmentC extends Fragment
{
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return view = inflater.inflate(R.layout.fragment_c,container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);


    }
}

activity_fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/parent_layout"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

    </FrameLayout>

</RelativeLayout>

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment A"
        android:textSize="15sp"/>

    <LinearLayout
        android:id="@+id/xLinLayFragmentContentB"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:orientation="vertical"/>

</LinearLayout>

fragment_b.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment B"
        android:textSize="15sp"/>

    <LinearLayout
        android:id="@+id/xLinLayFragmentContentC"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="vertical"/>

</LinearLayout>

fragment_c.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment C"
        android:textSize="15sp"/>


</LinearLayout>

enter image description here

答案 2 :(得分:0)

您应该在onActivityCreated()方法中调用嵌套片段,就像这样。

@Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        Fragment fragment = new FragmentC();
    //                  fragment.setArguments(bundle);
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.container_main, fragment);
                    fragmentTransaction.addToBackStack("my_fragment");
                    fragmentTransaction.commit();
}

答案 3 :(得分:0)

尝试实现默认的onDestroy(),如果你的片段中有onAttach(),那么删除它,可能会产生问题。我正在使用此片段调用替换片段。试试这个。可能它会帮助你:

    Fragment mFragment = null;
                    mFragment = new RadarMainActivity();
                    FragmentManager fragmentManager = getActivity()
                            .getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.container_body, mFragment).commit();