Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Kotlin - Sending data from activity back to fragment

When a user presses btnOpen in the FirstFragment, it'll create an activity. When clicks btnDone in SecondActivity, it should close the activity and pass back a String to the fragment.

FirstFragment.kt

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    btnOpen.setOnClickListener {
    var someActivityResultLauncher = registerForActivityResult(
        StartActivityForResult(),
        ActivityResultCallback<ActivityResult> { result ->
            println(result.data)
        })

    val intent = Intent(context, SecondActivity::class.java)
    activityLauncher.launch(intent)
    }
}

SecondActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    btnDone.setOnClickListener{
        val intent = Intent(this@InputAmountActivity,FirstFragment::class.java)
        intent.putExtra("Total","some data")
        finish()
    }
}

I'm getting null when attempting to print result.data. How exactly do I get the value of total from SecondActivity?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Right now you are creating an Intent and then just throwing it away without doing anything with it. You need to use setResult() to actually send it back to your first activity:

btnDone.setOnClickListener{
    val intent = Intent(this@InputAmountActivity,FirstFragment::class.java)
    intent.putExtra("Total","some data")
    setResult(Activity.RESULT_OK, intent)
    finish()
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!