[英]take data from child activity to main activity in kotlin
Hi all I'm new to android development(I'm using Kotlin) and startActivityForResult is deprecated trying to make one simple app To make it simple:大家好,我是android 开发的新手(我正在使用 Kotlin)并且不推荐使用 startActivityForResult 来尝试制作一个简单的应用程序为了简单起见:
I have a main activity with two button first activity and second activity.我有一个主要活动,有两个按钮第一个活动和第二个活动。
I have a "child" activity with a text fields and a finish button.我有一个带有文本字段和完成按钮的“子”活动。
When I click the main activity button, the child activity opens (it works data can also be access which i pass from main activity).当我单击主活动按钮时,子活动打开(它的工作数据也可以是我从主活动传递的访问)。 Then I put some text to the text fields and when I click the finish button I want the data from the text fields to be transferred to the main activity, and I can't get this working.
然后我将一些文本放入文本字段,当我单击完成按钮时,我希望将文本字段中的数据传输到主要活动,但我无法正常工作。 followed medium, yt and stackoverflow not able to make it work
遵循 medium、yt 和 stackoverflow 无法使其工作
my main activity code我的主要活动代码
package com.example.data
import android.app.Activity
import android.app.Instrumentation
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.preference.PreferenceManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
class MainActivity : AppCompatActivity() {
companion object{
private const val FIRST_ACTIVITY_RESULT = 1
}
lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val firstbtn = findViewById<Button>(R.id.first_activity_btn)
val first_text = findViewById<TextView>(R.id.first_activity_text)
val secondbtn = findViewById<Button>(R.id.second_activity_btn)
val edit = findViewById<EditText>(R.id.edit_name)
firstbtn.setOnClickListener{
var intent = Intent(this,first_activity::class.java)
intent.putExtra("data_name",edit.text.toString())
activityResultLauncher.launch(intent)
finish()
}
secondbtn.setOnClickListener{
var intent = Intent(this,second_activity::class.java)
startActivity(intent)
finish()
}
activityResultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()){ result:ActivityResult?->
if (result!!.resultCode == Activity.RESULT_OK){
if(result.data!!.extras!!.getString("data_transfer").toString() == "yes"){
Toast.makeText(applicationContext, "user send reply", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(applicationContext, "sorry user did not send reply", Toast.LENGTH_SHORT).show()
}
}
}
}
}
first activity code第一个活动代码
package com.example.data
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class first_activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
val finish = findViewById<Button>(R.id.finish_btn)
val textData = findViewById<TextView>(R.id.tv_first_activity)
val edit = findViewById<EditText>(R.id.edit_text_first_activity)
textData.text = intent.extras!!.getString("data_name")
finish.setOnClickListener{
// val intent = Intent(this,MainActivity::class.java)
// intent.putExtra("success","success message woohooo!!!")
// startActivity(intent)
val intent = Intent()
// val intent = Intent(this,MainActivity::class.java)
intent.putExtra("data_transfer",edit.text.toString())
setResult(Activity.RESULT_OK,intent)
// startActivity(intent)
finish()
}
}
}
Try to change the activityResultLauncher
declaration like this (initialize it before the onCreate
method):尝试像这样更改
activityResultLauncher
声明(在onCreate
方法之前对其进行初始化):
private val activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val response = result.data?.getStringExtra("data_transfer") ?: ""
if (response == "yes") {
Toast.makeText(applicationContext, "user send reply", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(applicationContext, "sorry user did not send reply", Toast.LENGTH_SHORT).show()
}
}
}
Also, remove the finish()
statement from the setOnClickListener
of the firstbtn
:此外,从
setOnClickListener
的firstbtn
中删除finish()
语句:
firstbtn.setOnClickListener{
var intent = Intent(this,first_activity::class.java)
intent.putExtra("data_name",edit.text.toString())
activityResultLauncher.launch(intent)
}
you need to use Object classes this.你需要使用 Object 类这个。
fields inside this class treat as static field in Java and they are availabe in whole of app, after finishing seconactivity in onRestart() method first activity you can get value which added to our Object (static) fields inside this class treat as static field in Java and they are availabe in whole of app, after finishing seconactivity in onRestart() method first activity you can get value which added to our Object (static)
make a object class and name it SharedValue and put below code inside that:制作一个 object class 并将其命名为SharedValue并将以下代码放入其中:
object SharedValues {
var myString = ""
}
MainActivity class:主要活动 class:
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
lateinit var txt:TextView
lateinit var btn:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn = findViewById(R.id.next_activity)
txt = findViewById(R.id.returned_text)
btn.setOnClickListener{
var intent = Intent(this,SecondActivity::class.java)
startActivity(intent)
}
}
override fun onRestart() {
super.onRestart()
if(SharedValues.myString !=""){
txt.text =SharedValues.myString
}
}
}
activity_main.xml: activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/next_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/returned_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your string appear here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity class:第二活动 class:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class SecondActivity : AppCompatActivity() {
lateinit var input:EditText
lateinit var btn:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
btn = findViewById(R.id.returned_text)
input = findViewById(R.id.my_input)
btn.setOnClickListener{
SharedValues.myString = input.text.toString()
finish()
}
}
}
activity_second.xml: activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<EditText
android:id="@+id/my_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/return_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="Return To FirstActivity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_input" />
</androidx.constraintlayout.widget.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.