I have a parent fragment ie FragmentA
and inflating a child fragment FragmentB
on it. In onCreateView function of FragmentB
trying to inflate another fragment `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);
....
Layout file:
<?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 ?
I am trying to use Handler
with postDelayed
with a 1 second interval then it is inflating FragmentC
& it is working fine. This looks not a solution.
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);