销毁具有层次关系的片段
Destroying fragments which are in a hierarchical relation
好吧,考虑一个片段有两个子片段的情况,例如在主片段中声明两个片段时 xml 如何销毁片段?是否会将所有三个片段都添加到返回堆栈中?
考虑主要片段 A 并考虑其 class 文件..
public class fragmentA extends Fragment {
public static FragmentC fragmentC;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(view!=null){
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try{
view = inflater.inflate(R.layout.fragment_a, container,
false);
fragmentC= (fragmentC) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.fragment_c);
}catch(InflateException e){
e.printStackTrace();
}
return view;
}
现在考虑片段 a 的 xml 文件 fragment_a
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/FragmentB"
android:name="com.example.FragmentB"
android:layout_width="320dp"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/FragmentC"
android:name="com.example.FragmentC"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:visibility="visible"
tools:layout="@layout/fragment_c" />
</LinearLayout>
在上面的场景中,如何销毁碎片??我们是否必须单独销毁所有三个碎片,或者我们是否必须只销毁碎片 a,其余的将自行销毁?所有的工作都在片段 B
中完成
答案很简单..
你所要做的就是在你离开页面时销毁每个片段
检查下面的代码
将此代码添加到两个页面..
pdmf 是对你 class(片段 A 或片段 C)的静态引用
然后当您从片段 B
移动时为两个片段调用此代码
public static void removeFragment() {
try {
FragmentTransaction ft = ((FragmentActivity) activityContext).getSupportFragmentManager()
.beginTransaction();
ft.remove((Fragment)pdMF);
ft.commit();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
好吧,考虑一个片段有两个子片段的情况,例如在主片段中声明两个片段时 xml 如何销毁片段?是否会将所有三个片段都添加到返回堆栈中?
考虑主要片段 A 并考虑其 class 文件..
public class fragmentA extends Fragment {
public static FragmentC fragmentC;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(view!=null){
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try{
view = inflater.inflate(R.layout.fragment_a, container,
false);
fragmentC= (fragmentC) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.fragment_c);
}catch(InflateException e){
e.printStackTrace();
}
return view;
}
现在考虑片段 a 的 xml 文件 fragment_a
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/FragmentB"
android:name="com.example.FragmentB"
android:layout_width="320dp"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_b" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/FragmentC"
android:name="com.example.FragmentC"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:visibility="visible"
tools:layout="@layout/fragment_c" />
</LinearLayout>
在上面的场景中,如何销毁碎片??我们是否必须单独销毁所有三个碎片,或者我们是否必须只销毁碎片 a,其余的将自行销毁?所有的工作都在片段 B
中完成答案很简单..
你所要做的就是在你离开页面时销毁每个片段 检查下面的代码 将此代码添加到两个页面.. pdmf 是对你 class(片段 A 或片段 C)的静态引用 然后当您从片段 B
移动时为两个片段调用此代码 public static void removeFragment() {
try {
FragmentTransaction ft = ((FragmentActivity) activityContext).getSupportFragmentManager()
.beginTransaction();
ft.remove((Fragment)pdMF);
ft.commit();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}