We are using a Fragment which extends BottomSheetDialogFragment.
public class XYZFragment extends BottomSheetDialogFragment implements BottomSheetController {
public static final String TAG = XYZFragment.class.getName();
private BottomSheetViewType mType;
private BottomSheetInitiatingSource mSource;
private BottomSheetCallback mCallback;
public void initilizeCallback(PSXBottomSheetCallback callback) {
mCallback=callback;
}
public enum BottomSheetViewType {
//some enum constants
}
public enum BottomSheetInitiatingSource {
//some enum constants
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
String type = getArguments().getString("type");
mType = BottomSheetViewType.valueOf(type);
String source = getArguments().getString("source");
mSource = BottomSheetInitiatingSource.valueOf(source);
// Use the data as needed
}
getActivity();
if (mCallback == null) {
mCallback = (PSXBottomSheetCallback) getActivity();
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
switch (mType) {
case CONSTANT_ONE:
return inflater.inflate(R.layout.layout1, container, false);
case CONSTANT_TWO:
return inflater.inflate(R.layout.layout2, container, false);
case CONSTANT_THREE:
return inflater.inflate(R.layout.layout3, container, false);
default:
return super.onCreateView(inflater, container, savedInstanceState);
}
}
//some more code
}
And this is how we are creating the Fragment:
XYZFragment fragment = new XYZFragment();
Bundle args = new Bundle();
args.putString("type", type.toString());
args.putString("source", source.toString());
fragment.setArguments(args);
fragment.show(fragmentManager, XYZFragment.TAG);
We are getting a crash with the following message:
The crash is happening in the onCreate of the activity which holds the fragment. Also the fragment is loaded dynamically based on the user action. The onCreate() method of the parent’s activity is called after which the stack trace looks like:
java.lang.Class.getConstructor0 (Class.java:2332)
java.lang.Class.getConstructor (Class.java:1728)
androidx.fragment.app.Fragment.instantiate (Fragment.java:663)
androidx.fragment.app.FragmentContainer.instantiate (FragmentContainer.java:57)
androidx.fragment.app.FragmentManager$3.instantiate (FragmentManager.java:507)
androidx.fragment.app.FragmentState.instantiate (FragmentState.java:81)
androidx.fragment.app.FragmentStateManager.<init> (FragmentStateManager.java:85)
androidx.fragment.app.FragmentManager.restoreSaveStateInternal (FragmentManager.java:2505)
androidx.fragment.app.FragmentManager.attachController (FragmentManager.java:2665)
androidx.fragment.app.FragmentController.attachHost (FragmentController.java:117)
androidx.fragment.app.FragmentActivity.lambda$init$3 (FragmentActivity.java:140)
androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable (ContextAwareHelper.kt:84)
androidx.activity.ComponentActivity.onCreate (ComponentActivity.java:377)
androidx.fragment.app.FragmentActivity.onCreate (FragmentActivity.java:217)
I tried with passing different fragment managers (child or parent) but still getting the crash. It is coming in nearly all android versions, but one thing I noticed was the average free ram was “1.1 GB”. This raised a possibility that the app is getting crashed when the OS released the resources
previously due to memory constraints and is trying to recreate the fragment.
I have tried the following steps to reproduce the crash:
- Use Do Not Keep Activities option inside developer options.
- Use FragmentScenario.recreate() to test the fragment.
Nothing worked. Really stuck here.
Prakash Rawat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.