场景:
MainActivity(我们将其称为活动A)具有3个片段,并开始了活动B。活动B然后是startActivityForResult(活动C)。问题是,在活动C上将结果设置为OK并调用onBackPress时,活动B上会调用startActivityForResult,但几秒钟后活动B将关闭。
活动A->活动B->活动C(用于结果),结果确定,完成
然后->活动B的onActivityResult被调用,结果为OK,但活动B完成并返回到ActivityA,后者重新加载了两次(我使用Logcat并看到两次为活动B和活动A调用了onDestroy和onCreate两次)。>
找到以下内容,但对我没有多大帮助Activity is closed after onActivityResult is called
试图查看launchMode
这是清单:
<activity
android:name=".activities.ActivityA"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden">
</activity>
<activity
android:name=".activities.ActivityB"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme.NoActionBar.Profile">
</activity>
<activity
android:name=".activities.ActivityC"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme.NoActionBar.Profile"
android:windowSoftInputMode="stateHidden|adjustPan">
</activity>
这是发布会:
活动A至活动B:
intent = Intent(this@ActivityA, ActivityB::class.java)
intent.putExtra("user", user)
startActivity(intent)
活动B至活动C:
val intent = Intent(this@ActivityB, ActivityC::class.java)
intent.putExtra("user", user)
活动C设置结果确定并完成:
setResult(Activity.RESULT_OK)
finish()
行为的截屏:https://youtu.be/gMlH5iujoh0
任何帮助表示赞赏
编辑:活动B onActivityResult
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_EDIT_PROFILE && resultCode == Activity.RESULT_OK) {
Timber.e("REQUEST_CODE_EDIT_PROFILE OK AND RESULT OK")
HelperTools(applicationContext).toastStatus(
getString(R.string.your_profile_has_been_updated),
Toast.LENGTH_SHORT,
ToastType.SUCCESS
)
populateData(user)
}
}
答案 0 :(得分:0)
val intent = Intent(this@ActivityB, ActivityC::class.java)
startActivityForResult(Intent(this@ActivityB,ActivityC::class.java),REQUEST_CODE_EDIT_PROFILE ))
请确认通过日志正确传输和接收了数据, 并找出导致onDestroy的原因
下面的示例代码用于获取可计算的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_CONTACT_SELECTION:
if (resultCode == Activity.RESULT_OK) {
assert data != null;
ContactModel mContactModel = data.getParcelableExtra("contactResult");
new MessageDialog(this)
.setMessage(getString(R.string.are_you_sure_you_want_to_block_contact, mContactModel.getFullName()))
.setPositiveButton(getString(R.string.yes), (dialog, which) -> {
dialog.dismiss();
API_blockUnblockContact(mContactModel, true);
})
.setNegativeButton(getString(R.string.no), (dialog, which) -> dialog.dismiss()).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
Logger.e(TAG, "Result Canceled");
}
break;
}
}