简体   繁体   中英

Remove a Fragment view and go back to Cordova Webview

I am creating a Cordova plugin based on the Android Camera2Video example http://developer.android.com/samples/Camera2Video/index.html

I have the Camera overlay showing and recording the video. However when I want to remove the view, it just shows a black screen.

Code to add the view:

Fragment fragment = Camera2VideoFragment.newInstance(cordova, callback);
cordova.getActivity().setContentView(resources.getIdentifier("activity_camera", "layout", packageName));
cordova.getActivity().getFragmentManager().beginTransaction().replace(resources.getIdentifier("container", "id", packageName), fragment).commit();

Code to remove the view:

Fragment fragment = cordova.getActivity().getFragmentManager().findFragmentById(resources.getIdentifier("container", "id", packageName));
cordova.getActivity().getFragmentManager().beginTransaction().remove(fragment).commit();

How can I remove the fragments and return to the regular WebView?

My plugin code so far is here: https://github.com/kmturley/cordova-plugin-media-custom

Ok so I managed to solve it using the following code:

Add the view:

cordova.getActivity().setContentView(resources.getIdentifier("activity_camera", "layout", packageName));
cordova.getActivity().getFragmentManager().beginTransaction().replace(resources.getIdentifier("container", "id", packageName), Camera2VideoFragment.newInstance(cordova, callback)).commit();

Remove the view:

Fragment fragment = cordova.getActivity().getFragmentManager().findFragmentById(resources.getIdentifier("container", "id", packageName));
cordova.getActivity().getFragmentManager().beginTransaction().remove(fragment).commit();
cordova.getActivity().setContentView(getView());

I copied the getView function from the cordova-plugin-splashscreen repo: https://github.com/apache/cordova-plugin-splashscreen/blob/master/src/android/SplashScreen.java

private View getView() {
    try {
        return (View)webView.getClass().getMethod("getView").invoke(webView);
    } catch (Exception e) {
        return (View)webView;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM