Sam Chenon Stack Overflow • Recommends
On “Is there a way to pass a function reference between activities?”
2022 Update:
My original approach has a drawback, in my demo if I press the home button to put the app into the background, it will crash with NotSerializableException .
Actually there is another API just for listening changes from one Fragment in the other, that is FragmentResultListener , offical tutorial is here: link to this video.
If FragmentResultListener cannot meet your need, Shared ViewModel is the final solution in there: link to this video.
1. You need to add id 'kotlin-parcelize' to your build.gradle (app), like this:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-parcelize' //here
}
2. Create a Class to wrap your Lambda function:
@Parcelize…