[英]Null intent in onActivityResult(int requestCode, int resultCode, Intent data)
[英]Returning intent always received as resultCode 0 and Intent data is null
我有兩個應用程序,我試圖互相交談。 App 1 is an old app coded in Java in Android Studio App 2 has been written in Xamarin.Forms but currently only coded for Android
應用 1 包含它嘗試使用來自應用 2 的信息更改的信息。應用 1 通過以下 function 調用應用 2:
private void launchApp(String packageName) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
String locationID = mCurrentJob.locationDetailDto.location;
String taskID = String.valueOf(mCurrentJob.id);
String userID = String.valueOf(Global.myUser.id);
String deviceID = Urls.DEVICE_ID;
if (launchIntent != null) {
try {
launchIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
launchIntent.putExtra("LocationID", locationID);
launchIntent.putExtra("TaskID", taskID);
launchIntent.putExtra("UserID", userID);
launchIntent.putExtra("DeviceID", deviceID);
launchIntent.setFlags(0);
launchIntent.setType("text/plain");
startActivityForResult(launchIntent, LAUNCH_APP);
} catch (ActivityNotFoundException error) {
TelemetryLog.error(TAG, Statics.ERROR_MESSAGE_TRY_CATCH_BLOCK + error, error);
}
} else {
TelemetryLog.debug(TAG, "[launchApp][Error]");
}
}
這是同一文件/活動中的接收器代碼:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// COMPLETION ACTIVITY
if (resultCode == RESULT_OK) {
switch (requestCode) {
case LAUNCH_APP:
String intentAssetID = Util.clean(data.getStringExtra("ID"));
String intentAssetDescription = Util.clean(data.getStringExtra("Description"));
String assetBarCode = Util.clean(data.getStringExtra("Barcode"));
String assetNumberRef = Util.clean(data.getStringExtra("AssetNumber"));
TelemetryLog.debug(TAG, "[onActivityResult]" + intentAssetID);
TelemetryLog.debug(TAG, "[onActivityResult]" + intentAssetDescription);
//If there is not (id + desc) and no barcode and no assetNumberRef
if ((TextUtils.isEmpty(intentAssetID) || TextUtils.isEmpty(intentAssetDescription)) && TextUtils.isEmpty(assetBarCode) && TextUtils.isEmpty(assetNumberRef)) {
Toast.makeText(mContext, mContext.getString(R.string.completion_activity_no_asset_from_app), Toast.LENGTH_LONG).show();
} else if (isPlanned) {
Toast.makeText(mContext, "Asset cannot be changes on a PPM job.", Toast.LENGTH_LONG).show();
} else {
try {
if (!TextUtils.isEmpty(intentAssetID)) {
long id = Long.parseLong(intentAssetID);
mAssetUpdateRequired = true;
mNewAssetId = id;
mNewAssetDesc = intentAssetDescription;
showFieldsForNewAsset();
} else {
//The Asset app did not return an asset ID - it may have returned a barcode or reference
if (!TextUtils.isEmpty(assetBarCode)) {
mAssetUpdateRequired = true;
mNewAssetId = -1;
mAssetBarCode = assetBarCode;
mNewAssetDesc = "Barcode: " + assetBarCode; //Use asset barcode for displayed description
showFieldsForNewAsset();
} else if (!TextUtils.isEmpty(assetNumberRef)) {
mAssetUpdateRequired = true;
mNewAssetId = -1;
mAssetNumRef = assetNumberRef;
mNewAssetDesc = "Asset Number: " + assetNumberRef; //Use asset number ref for displayed description
showFieldsForNewAsset();
}
}
} catch (Exception e) {
Toast.makeText(mContext, mContext.getString(R.string.completion_activity_no_asset_from_app), Toast.LENGTH_LONG).show();
TelemetryLog.error(TAG, "[onActivityResult][LAUNCH_APP] " + Statics.ERROR_MESSAGE_TRY_CATCH_BLOCK + e, e);
}
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
}
在 App 2 上正確接收信息,提供選項列表,選擇一個,按下按鈕后,所選項目將轉換為所需的格式 model。 然后我使用 MessagingCenter 將它從 Forms 端傳遞到 Android 端,這是接收它並回調到 App 1 的地方:
protected override void OnStart()
{
base.OnStart();
// App should return here after Submitting an Asset from the DetailsPage
Xamarin.Forms.MessagingCenter.Subscribe<DetailsPage, SendAsset>(this, "Asset", (sender, arg) =>
{
SendAsset asset = arg;
if (asset != null)
{
ReturnToCaller(asset);
}
});
}
public void ReturnToCaller(SendAsset assetDetails)
{
Intent returnAsset = new Intent(Intent.ActionSend);
returnAsset.PutExtra("ID", assetDetails.ID);
returnAsset.PutExtra("AssetNumber", assetDetails.Code);
returnAsset.PutExtra("Description", assetDetails.Description);
returnAsset.PutExtra("Barcode", assetDetails.Barcode);
returnAsset.PutExtra("Serial", assetDetails.Serial);
SetResult(Result.Ok, returnAsset);
Finish();
}
現在,所有這些代碼都可以正常工作,直到它到達 App 1 上的“onActivityResult”。我已經通過這些步驟進行了調試,調整了兩端意圖的多個部分(例如,刪除標志、刪除類型、指定類型),我可以看到返回意圖符合預期。 它填充了 Extras 並標有 ReultCode.OK (-1),但每次到達 App 1 時,resultCode 始終為 0,並且在調用 App 2 時具有匹配的 requestCode。
我錯過了什么明顯的東西嗎? 還是有一些更深層次的路由問題? 任何建議都非常感謝,因為我非常堅持這一點,在此先感謝。
注意:我已經清理了代碼以適合此處,並刪除了不相關的部分或只是簡單地注釋掉並更改了一些 naes,以便代碼保持匿名。
更新:所以我仍然遇到一些麻煩,但我發現我總是收到 resultCode 0 和 NULL 意圖數據的原因是因為我從 MainActivity 而不是設置為 MainLauncher 的 SplashScreen 調用 SetResult 和 Finish為應用程序。 現在它將返回正確的結果,但僅在處理 SplashScreen 期間調用。 如果我嘗試使用 MessagingCenter 訂閱來自獲取詳細信息的 Forms 頁面的響應,則會收到兩次消息並且不會關閉應用程序(因此根本不會返回到應用程序 1)
我正在嘗試找到訂閱此 MessagingCenter 的正確方法,但似乎無論我將訂閱和取消訂閱放在應用程序生命周期中的哪個位置,它都會調用兩次或根本不調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.