将数据从 child activity onActivityResult() 发送到 parent activity onActivityResult()
Sending data from a child activity onActivityResult() to parent activity onActivityResult()
我有一个包含 2 个 Activity 的应用程序。 Activity A (parent) 启动 Activity B (child) 通过调用:
startActivityForResult(intentB,B_Request);
Activity B是条码扫描仪。 (我使用这个库:com.journeyapps:zxing-android-embedded:3.2.0@aar)。
在此 activity 中,我必须覆盖 onActivityResult() 方法才能获取扫描代码。之后我想将 'code' 中存储的数据发送回 parent (Activity A) onActivityResult()。我是这样做的:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String code = null;
if(result != null){
if(result.getContents() == null){
return;
}
else {
code = result.getContents();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
Intent intent = new Intent(getApplicationContext(),A.class);
intent.putExtra("scannedCode", code);
setResult(RESULT_OK, intent);
finish();
}
parent onActivityResult() 看起来像这样:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
String scannedCode = data.getStringExtra("scannedCode");
toastMessage("Scanned code: " + scannedCode);
}
}
}
我得到以下异常,我没有看到 Activity A,而是被重定向到 Activitie 的 A parent。
E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:24782)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24902)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24879)
at com.android.server.am.ActivityStackSupervisor.ensureVisibilityAndConfig(ActivityStackSupervisor.java:1671)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1420)
at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1709)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:3043)
at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2488)
at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:2234)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1745)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1669)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:9657)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:224)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
at android.os.Binder.execTransact(Binder.java:752)
p.s。我获得 Activity B 中的代码还不够,我需要 Activity A 中的代码。
你能告诉我我做错了什么吗?
谢谢!
只需输入 new intent() 而不是
new Intent(getApplicationContext(),A.class)
在你的childactivity
我有一个包含 2 个 Activity 的应用程序。 Activity A (parent) 启动 Activity B (child) 通过调用:
startActivityForResult(intentB,B_Request);
Activity B是条码扫描仪。 (我使用这个库:com.journeyapps:zxing-android-embedded:3.2.0@aar)。 在此 activity 中,我必须覆盖 onActivityResult() 方法才能获取扫描代码。之后我想将 'code' 中存储的数据发送回 parent (Activity A) onActivityResult()。我是这样做的:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String code = null;
if(result != null){
if(result.getContents() == null){
return;
}
else {
code = result.getContents();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
Intent intent = new Intent(getApplicationContext(),A.class);
intent.putExtra("scannedCode", code);
setResult(RESULT_OK, intent);
finish();
}
parent onActivityResult() 看起来像这样:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
String scannedCode = data.getStringExtra("scannedCode");
toastMessage("Scanned code: " + scannedCode);
}
}
}
我得到以下异常,我没有看到 Activity A,而是被重定向到 Activitie 的 A parent。
E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:24782)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24902)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24879)
at com.android.server.am.ActivityStackSupervisor.ensureVisibilityAndConfig(ActivityStackSupervisor.java:1671)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1420)
at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1709)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:3043)
at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2488)
at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:2234)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1745)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1669)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:9657)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:224)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
at android.os.Binder.execTransact(Binder.java:752)
p.s。我获得 Activity B 中的代码还不够,我需要 Activity A 中的代码。
你能告诉我我做错了什么吗?
谢谢!
只需输入 new intent() 而不是
new Intent(getApplicationContext(),A.class)
在你的childactivity