0

I have an empty MutableList of 'Exercises' in my MainActivity.

val exercisesList : MutableList<Exercise> = arrayListOf()

I then open a new Activity with

val intent = Intent(context, ExerciseCatalogueActivity::class.java)
startActivity(intent)

In the new activity I have a RecyclerView of Exercises, where I want on click of an element to close the activity and pass the Exercise back into the MainActivity's Mutable list of Exercises.

Alex Petev
  • 501
  • 5
  • 19

3 Answers3

2

You can use startActivityForResult instead of startActivity.

Here is example

class MainActivity : Activity(){
    const val REQUEST_CODE = 10001

    override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)

          setContentView(R.layout.activity_main)

          // your implementation
          Intent(this,SomeActivity::class.java).apply {
                // add your data to intent
                startActivityForResult(this,REQUEST_CODE)
          }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
             REQUEST_CODE -> handleResult(resultCode,data)
             else ->super.onActivityResult(requestCode,resultCode,data)
        }
    }

    fun handleResult(result:Int,data:Intent?){
        if(result!=Activity.RESULT_OK) return
        if(data == null) return

        val someData = data.getSerializableExtra("key")
        // do your stuff with someData
    }
}

in your SecondActivity you should use setResult method to pass your data to caller activity( or MainActivity in this case).

class SecondActivity : Activity(){

     // your implementation of activity

    fun sendSuccessResult(someData:Exercise){
        setResult(Activity.RESULT_OK,Intent().apply{ putExtra("key",someData) })
        finish()
    }
}

By calling sendSuccessResult and passing your data, the data will be send back to your MainActivity.

You can read more about it here https://developer.android.com/reference/android/app/Activity

Shayan D
  • 620
  • 3
  • 14
  • I think this answer seems better. Instead of implementing serialization or parcelable, you can basically 'extract' out the object's property and create a new `Excercise` object and set the values. Although, not the prettiest way to deal with it. – Taseer Oct 07 '19 at 16:04
  • For some reason finish() isn't closing my `SecondActivity` – Alex Petev Oct 07 '19 at 16:07
  • 1
    You should use `finishActivity(resultCode)` – Taseer Oct 07 '19 at 17:17
  • @TaseerAhmad `finishActivity` will cause another activity to close and it's input is `RequestCode` not `resultCode`. So it cannot be used here. – Shayan D Oct 09 '19 at 07:31
  • @AlexPetex `finish()` method will not close activity immediately and closing activity depends on lifecycle status. but if this method will not finish your activity at all you can call 'onBackPresssed` method. its not a good way at all but will fix your problem. – Shayan D Oct 09 '19 at 07:36
0

You should startActivityForResult see https://developer.android.com/training/basics/intents/result

You could pass you object to and from the Activity as Parcelable data

https://developer.android.com/reference/android/content/Intent#putExtra(java.lang.String,%20android.os.Parcelable)

To make your object Parcelable

How can I make my custom objects Parcelable?

(Could also use Serializable to pass a serialized object but less Android like)

Andrew
  • 8,198
  • 2
  • 15
  • 35
0

You can put your exercisesList in the companion object of your MainActivity, then call it from another activity; like static variables in Java, if you know that language.

In this way:

MainActivity.kt:

class MainActivity : Activity { // don't mind if this is different
    ...
    companion object {
        val exercisesList : MutableList<Exercise> = arrayListOf()
    }
}

ExerciseCatalogueActivity.kt:

class ExerciseCatalogueActivity : Activity { // same as before

    ... onCreate (...) {
        MainAcitivity.exercisesList.doWhatYouWant
    }
}
Luca Murra
  • 1,848
  • 14
  • 24