Home > Software design >  How to finish an activity inside onSuccess Realm callback?
How to finish an activity inside onSuccess Realm callback?

Time:10-14

I'm trying to call the finish() method inside the onSuccess() callback of a Realm transaction. The MainActivity opens another activity for the user to enter the data and create an object, this after clicking a button, my code looks something like this:

addButton.setOnClickListener {
  val newObject = MyObject()
  newObject.name = "Name"
  
  realmThread.executeTransactionAsync(
    { transaction -> transaction.insert(newObject) },
    { _ -> finish() }
  )

}

the transaction is made, but the activity doesn't close.

CodePudding user response:

*Make a method to call after a response from AsyncTasc and use in it finish(). I guess it doesn't work for you because you call it in a separate thread *

CodePudding user response:

So the solution was to specify that I'm implementing the onSuccess callback:

realmThread.executeTransactionAsync(
  { transaction -> 
    transaction.insert(newObject)
  },
  Realm.Transaction.OnSuccess {
    finish()
  }
)
  • Related