在Android Studio中用Kotlin进行开发。实现如下功能:在SecondActivity中,用户通过按下Back键回到FirstActivity,同时,要把SecondActivity的数据data2传回给FirstActivity。 请提供完整代码,采用当前主流的方法进行实现。
在Android Studio中使用Kotlin实现从SecondActivity返回FirstActivity并传递数据,可以使用startActivityForResult
和setResult
方法。以下是完整的代码示例:
package com.example.myapp
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
class FirstActivity : AppCompatActivity() {
companion object {
const val REQUEST_CODE = 1
const val EXTRA_DATA2 = "extra_data2"
}
private lateinit var resultTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
resultTextView = findViewById(R.id.resultTextView)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivityForResult(intent, REQUEST_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val data2 = data?.getStringExtra(EXTRA_DATA2)
resultTextView.text = data2
}
}
}
package com.example.myapp
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
class SecondActivity : AppCompatActivity() {
private lateinit var dataEditText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
dataEditText = findViewById(R.id.dataEditText)
val backButton = findViewById<Button>(R.id.backButton)
backButton.setOnClickListener {
val data2 = dataEditText.text.toString()
val resultIntent = Intent()
resultIntent.putExtra(FirstActivity.EXTRA_DATA2, data2)
setResult(Activity.RESULT_OK, resultIntent)
finish()
}
}
override fun onBackPressed() {
val data2 = dataEditText.text.toString()
val resultIntent = Intent()
resultIntent.putExtra(FirstActivity.EXTRA_DATA2, data2)
setResult(Activity.RESULT_OK, resultIntent)
super.onBackPressed()
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Result will be shown here" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/dataEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter some data" />
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Back to First Activity" />
</LinearLayout>
这样,当用户在SecondActivity中输入数据并返回FirstActivity时,数据会被传递并显示在FirstActivity中。