ฉันต้องการส่งคืนข้อมูลบางส่วนจากกิจกรรมลูกของฉัน ActivityChild
ไปยังกิจกรรมหลักของฉัน ActivityMain
ก่อนอื่นฉันจะโทรหา ActivityChild
จาก ActivityMain
ของฉันด้วยวิธีนี้:
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivityForResult(intent, 1)
// Actually, this code first launch chrome, then when the user identify on
// chrome, the redirect_uri calls the ActivityChild
จากนั้นฉันจะคืนข้อมูลของฉันจาก ActivityChild
ถึง ActivityMain
ด้วยวิธีนี้:
val data: Uri? = intent.data
val returnedIntent = Intent();
returnedIntent.putExtra("code", data!!.getQueryParameter("code"))
setResult(Activity.RESULT_OK, returnedIntent)
finish()
และในที่สุดฉันก็จับข้อมูลด้วยการแทนที่ onActivityResult ใน ActivityMain
:
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
val returnedValue = data!!.getStringExtra("data")
}
else if (resultCode == Activity.RESULT_CANCELED)
Log.d("mDebug", "Couldn't retrieve data") // Always enters here
else
Log.d("mDebug", "final")
}
}
มันจะดำเนินต่อไปในเงื่อนไข Activity.RESULT_CANCELED
ฉันไม่สามารถแก้ไขปัญหานี้ได้ หากสามารถแก้ไขได้
ฉันเชื่อว่าสาเหตุมาจากการที่ฉันเปิด Chrome เป็นครั้งแรกด้วยความตั้งใจ และไม่สามารถถ่ายโอนข้อมูลไปยัง ActivityMain
ได้ด้วยวิธีใดก็ตาม ตอนนี้ฉันยังห่างไกลจากทุกสิ่งที่เกี่ยวข้องกับเจตนาและฉันอาจจะพลาดบางสิ่งบางอย่างที่นี่ ดังนั้นหากใครสามารถช่วยฉันและอธิบายได้โปรด!