我有一个使用https://github.com/googlesamples/android-Camera2Video示例的Cordova插件。我想在相机中禁用后退按钮。
要做到这一点,我相信Fragments不能接受后退按钮事件,所以已经为插件添加了一个Activity。我可以成功运行插件,相机和活动,禁用后退按钮。
然而,当我关闭插件时,它出现错误:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
为了解决这个问题,我使用了commitAllowingStateLoss
而不是commit
但是当我第二次打开插件时,它出错了:
java.lang.NullPointerException: Attempt to invoke interface method 'android.app.Activity org.apache.cordova.CordovaInterface.getActivity()' on a null object reference
我认为我没有正确停止活动,并且允许状态丢失,这会导致第二个错误。我该如何解决这个问题?
我尝试过这种方法,但没有运气: PhoneGap Android Plugin - close the plugin Activity
我已将我的代码放在此处的分支中: https://github.com/kmturley/cordova-plugin-media-custom/tree/feature/back-button
添加更改摘要:
的plugin.xml
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="com.example.android.camera2video.MediaCustomActivity" android:label="@string/activity_name" android:theme="@style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</config-file>
<source-file src="src/android/MediaCustom.java" target-dir="src/com/example/android/camera2video" />
<source-file src="src/android/MediaCustomActivity.java" target-dir="src/com/example/android/camera2video" />
<source-file src="src/android/Camera2VideoFragment.java" target-dir="src/com/example/android/camera2video" />
MediaCustom.java
public void show() {
if (cameraFragment != null) {
return;
}
cameraFragment = Camera2VideoFragment.newInstance(cordova, callbackContext);
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
cordova.getActivity().setContentView(resources.getIdentifier("activity_camera", "layout", packageName));
FragmentManager fragmentManager = cordova.getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(resources.getIdentifier("container", "id", packageName), cameraFragment);
fragmentTransaction.commit();
MediaCustomActivity.start(cordova.getActivity());
}
});
}
public void hide() {
if (cameraFragment == null) {
return;
}
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
MediaCustomActivity.stop(cordova.getActivity());
cordova.getActivity().setContentView(getView());
FragmentManager fragmentManager = cordova.getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(cameraFragment);
fragmentTransaction.commitAllowingStateLoss(); // commit();
cameraFragment = null;
}
});
}
MediaCustomActivity.java
public static void start(Activity activity) {
Intent intent = new Intent(activity, MediaCustomActivity.class);
activity.startActivity(intent);
}
public static void stop(Activity activity) {
// what can we do here?
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}