The following snippet shows how OpenCV4Android is initialized in an Activity
, e.g. in this sample app, but I need to do it inside a Fragment
(because my JavaCameraView
is in my Fragment
).
I would do it just like that have done in the Activity
, i.e. I would initialize baseLoaderCallback
directly inside the class body (outside any method's body), but this way, the initializing code (4th statement in the following snippet) would be executed as soon as the Fragment
is instantiated.
As we can see, the BaseLoaderCallback
constructor takes application context as an argument, and I can't call getActivity()
before onActivityCreated()
is called, because before onActivityCreated()
, getActivity()
can return null
.
public class AnActivity extends Activity implements CvCameraViewListener2 {
...
private JavaCameraView javaCameraView;
...
private CameraBridgeViewBase cameraBridgeViewBase;
BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(getActivity().getApplicationContext()) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
cameraBridgeViewBase.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
...
//SOME LIFE CYCLE CALL BACK METHODS
@Override
public void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, getActivity().getApplicationContext(),
baseLoaderCallback);
}
...
}