gpt4 book ai didi

android - 子 fragment 正在自动销毁

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:33 26 4
gpt4 key购买 nike

我有一个父 fragment ,即 FragmentA 并在其上膨胀一个子 fragment FragmentB。在 FragmentB 的 onCreateView 函数中试图膨胀另一个 fragment “FragmentC”。

fragment A.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);
}
}

fragment B.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);
}

}

fragment C.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 ?

我正在尝试以 1 秒的间隔将 HandlerpostDelayed 一起使用,然后它正在膨胀 FragmentC 并且工作正常。这看起来不是解决方案。

        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);

最佳答案

我以我的方式做到了,它没有破坏 Fragment 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();
}

}

fragment A.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();

}
}

fragment B.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();
}
}

fragment C.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

关于android - 子 fragment 正在自动销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com