Issue
[Beginner] I added an Edit Text on 2 Activities and Intents to share information between them. From 1 to 2 I send question (pop-ups on TextView), from 2 to 1 I send the answer (pop-ups on TextView).
The second part works perfect, but I can't for some reason send the question. What the hеck
MainActivity1 :
class MainActivity : AppCompatActivity() {
private lateinit var bq: ActivityMainBinding
private lateinit var runner: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bq = ActivityMainBinding.inflate(layoutInflater)
setContentView(bq.root)
runner = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{result: ActivityResult ->
if (result.resultCode == RESULT_OK) {
val text = result.data?.getStringExtra("Go1")
bq.t2.text = text
}
}
}
fun onClickGo2 (view: View) {
val i = Intent(this, MainActivity2 :: class.java)
i.putExtra("Go2", bq.ed1.text.toString())
setResult(RESULT_OK, i)
runner.launch(i)
}
}`
MainActivity2 :
`class MainActivity2 : AppCompatActivity() {
private lateinit var bq: ActivityMain2Binding
private lateinit var runner2: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bq = ActivityMain2Binding.inflate(layoutInflater)
setContentView(bq.root)
runner2 = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{result: ActivityResult ->
if (result.resultCode == RESULT_OK) {
Log.d("MyLog", "Результат ${result.resultCode}")
val text = result.data?.getStringExtra("Go2").toString()
bq.t4.text = text
}
}
}
fun onClickGo1 (view: View) {
val i = Intent()
i.putExtra("Go1", bq.ed2.text.toString())
setResult(RESULT_OK, i)
finish()
}
}
Solution
First I will give my reasoning, and at the end the solution.
When we use a contract (registerForActivityResult
, etc.), we mean that we want to get a result from the Activity
for which we are "launching
". That is why the information from the second Activity
to the first one has successfully passed and applied to the TextView
.
You have a problem with the fact that the data from the first Activity
was not transferred to the second Activity
. In fact, they were transferred with Intent
, but since the Contract operates unilaterally (as far as I understand), it simply cannot process this data.
The solution to your problem is to use the usual data reception from the Intent
in the second Activity
, i.e. the simplest is this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
runner2 = registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{...}
bq.t4.text = intent.getStringExtra("Go2") // That's what you need
}
That's all.
You can also experiment with the contract. For example, your example code above (original). Just replace your fun OnClickGo1
code with:
fun onClickGo1 (view: View) {
val i = Intent(this, MainActivity::class.java)
i.putExtra("Go1", bq.ed2.text.toString())
setResult(RESULT_OK, i)
runner2.launch(i)
}
See, the first time you click on the transition to MainActivity2
(fun onClickGo2
), you also ask it for data back if it has any.
Next, you click on the element again with onClickGo1
, so that, according to the new code from MainActivity2
, go to a new MainActivity instance
and immediately conclude a contract so that it (MainActivity new instance
) gives some data back to the old MainActivity2 instance
.
Next, click again on the onClickGo2
element to navigate to the new instance of MainActivity2
.
And here is the most interesting. You have created 4 contracts so far, three of which will work as you wish. Press the back button
on the phone, and you will see that the textView
of all previous Activities
is filled successfully.
Looks like this (*MA - MainActivity
):
MA1(no data) -> MA2 (no data) -> MA1_1 (no data) -> MA2_1 (no data)
Next press back button on phone: MA2_1 (no data) (click back) -> MA1_1 (filled) (click back) -> MA2 (filled) (click back) -> MA1 (filled) (click back)
Good luck.
Answered By - ionnomirai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.