我正在使用react-native-share-extension从图库中共享图像。所以我使用了两个活动(MainActivity 和 ShareActivity)。
但是当 ShareActivity 完成时,主应用程序没有任何反应。所有 UI 组件都已停止。
Android 监视器消息显示“无法向 JS 发送触摸,因为尚未附加催化剂实例”
我的代码如下。
AndroidManifest.xml
<activity
android:noHistory="true"
android:name=".share.MyShareActivity"
android:configChanges="orientation"
android:label="@string/title_activity_share"
android:screenOrientation="portrait"
android:launchMode="standard"
android:theme="@style/Theme.Share.Transparent" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
MyShareActivity.java
package com.mobileopenalm.share;
import android.app.Activity;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
import com.github.alinz.reactNativeShareExtension.ShareExActivity;
import com.mobileopenalm.BuildConfig;
public class MyShareActivity extends ReactActivity {
static MyShareActivity myActivity = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myActivity = this;
}
@Override
protected String getMainComponentName() {
return "MyShareEx";
}
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
static protected MyShareActivity getActivity(){
return myActivity;
}
}
InternalShare.java(声明 reactMethod)
public class InternalShare extends ReactContextBaseJavaModule {
public InternalShare(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "ShareExtension";
}
@ReactMethod
public void close() {
MyShareActivity.getActivity().finish();
}
@ReactMethod
public void data(Callback successCallback){
// WritableMap map = Arguments.createMap();
Intent intent = getCurrentActivity().getIntent();
String action = intent.getAction();
String type = intent.getType();
String[] value = null;
if (type == null) {
type = "";
}
//if you want to support more, just add more things here.
//at the moment we are only supporting browser URL
if (Intent.ACTION_SEND.equals(action)) {
ClipData c = intent.getClipData();
int count = c.getItemCount();
value = new String[count];
for(int i = 0; i < count ; i++){
value[i] = c.getItemAt(i).getUri().toString();
}
}
if(value == null){
successCallback.invoke("");
} else {
successCallback.invoke(value);
}
}
}
share.android.js - 关闭方法(共享扩展从这里开始。)
onClose() {
// console.log(NativeModules.ShareExtension.data())
NativeModules.ShareExtension.close();
},
index.android.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
import Share from './share.android';
'use strict';
var { AppRegistry } = require('react-native');
var App = require('./App/App');
// var DropDownApp = require('./App/DropDown');
AppRegistry.registerComponent('mobileOpenAlm', () => App);
AppRegistry.registerComponent('MyShareEx', () => Share);
细节 -
当我从图库中分享照片时,应用重定向了 share.android.js。主应用程序(mobileOpenAlm)进入后台。
在 share.android.js 使用 onClose() 方法后,应用程序成功关闭。但我回到主应用程序,应用程序没有响应。
日志如下。
由于尚未附加催化剂实例,因此无法将触摸分派给 JS。
我该如何解决这个问题?