[英]Move Data from Second Activity to first Activity
I have an Activity which has an empty Text, you can click add item to add text to the Text, so when I try to add the data from second Activity
to main back the App won't even launch. 我有一个活动,该活动的文本为空,您可以单击添加项目以将文本添加到文本,因此当我尝试将第二个
Activity
的数据添加到主文本时,该应用程序甚至无法启动。
MainActivity 主要活动
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView1: TextView = findViewById(R.id.num1)
val textView2: TextView = findViewById(R.id.num2)
val Button: Button = findViewById(R.id.button)
val intent = intent
val message: String = intent.getStringExtra(SecondActivity.EXTRA_MESSAGE)
textView1.setText(message)
Button.setOnClickListener(){
val intent: Intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
SecondActivity: SecondActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val item = edit.text.toString()
button.setOnClickListener(){
val intent = Intent(this, MainActivity::class.java)
intent.putExtra(EXTRA_MESSAGE , item)
startActivity(intent)
}
}
companion object {
val EXTRA_MESSAGE: String =
"com.example.android.twoactivities.extra.MESSAGE"
}
To receive data from Second activity after intending from first activity.
打算从第一个活动接收后从第二个活动接收数据。
Step 1 : launch Second Activity using Intent with startActivityForResult(Intent intent, int REQUEST_CODE) by doing this, you will receive the data send from second activity步骤1:通过将Intent与startActivityForResult(Intent intent,int REQUEST_CODE)一起使用Intent启动Second Activity ,您将收到从第二Activity发送的数据
val intent: Intent = Intent(this, SecondActivity::class.java) startActivityForResult(intent, 101) val intent:Intent = Intent(this,SecondActivity :: class.java)startActivityForResult(intent,101)
Step 2 : Open Second Activity and write the below code where you want to finish the Second Activity (ie, where you want to send data to First Activity ) Note : "KEY" and the data-type passing is important to get the result in First Activity
第2步:打开Second Activity,然后在要完成Second Activity的位置 (即要将数据发送到First Activity的位置 )编写以下代码: 注意 :“ KEY”和数据类型传递对于获取结果很重要第一次活动
val value : String = "sendBackData"
val intent = Intent()
resultIntent.putExtra("KEY", value)
setResult(Activity.RESULT_OK, intent)
finish()
Step 3 : In First activity.
步骤3:在第一个活动中。 create override method onActivityResult to get the data into First Activity with "KEY"
创建重写方法onActivityResult以使用“ KEY”将数据获取到First Activity中
Note : The key at Second Activity and Key inside onActivityResult of First Activity should be same.
注:内第一个活动的onActivityResult的第二个活动的关键和重点应该是相同的。
override fun onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 101 && resultCode == RESULT_Ok && data != null){
val str : String = data.getStringExtra("KEY")
}
}
you need to call 你需要打电话
startActivityForResult(intent, request_code) ;
instead of 代替
startActivity(intent);
in order to receive the response in onActivityResult method. 为了在onActivityResult方法中接收响应。
Hi use startActivityForResult(intent, requestcode);
嗨,请使用
startActivityForResult(intent, requestcode);
and in your SecondActivty just use this 并在您的SecondActivty中使用此
val myintent = Intent()
resultIntent.putExtra("YourKey", value)
setResult(Activity.RESULT_OK, myintent)
finish()
to retrun to your MainActivity. 重新运行到您的MainActivity。
First of all, from MainActivity to SecondActivity you need to use startActivityForResult() 首先,从MainActivity到SecondActivity,您需要使用startActivityForResult()
val requestCode = 0
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("your key","your value")
this.startActivityForResult(intent, requestCode)
you declare a request code, an integer, declare the intent like you are doing now, and put your key in the intent put extra. 您声明一个请求代码(一个整数),像现在一样声明意图,然后将关键字放入意图中,并添加额外的内容。 The important part: You need to use startActivityForResult with your intent and the request code.
重要部分:您需要将startActivityForResult与您的意图和请求代码一起使用。
Now, in the second activity, to get your intent string: 现在,在第二个活动中,获取您的意图字符串:
val string = intent.getStringExtra("your key")
When you are ready to return a value, use: 准备好返回值时,请使用:
val resultCode = 1
val returnIntent = Intent()
returnIntent.putExtra("your key","your value")
setResult(resultCode, intent)
finish()
Where you create a new intent, you put what you need to put in the intent extras, and you return it with setResult - Remember the resultCode and the requestCode. 在创建新的Intent的地方,将需要放入的Intent Extras放入其中,并用setResult返回-记住resultCode和requestCode。 The finish() is to close the activity you are currently in (only set result won't close the activity)
finish()用于关闭您当前所在的活动(仅设置结果不会关闭该活动)
Finally, on the main activity you need to override this method: 最后,在主要活动上,您需要重写此方法:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == yourRequestCode){
if (resultCode == yourResultCode){
//do something
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.