Search code examples
androidkotlinandroid-fragmentsfragmentandroid-fragmentactivity

Kotlin: Is it possible to move from Fragment of Activity-1 to Activity-2


I'm beginner in Android development. I'm creating an application in Kotlin and met some problem. I wanted to show another activity and get the selected data from it to the fragment but I could not find how to do that...

Language: Kotlin Problem: Is it possible to do as shown on picture A? If yes, may I get some advice of how to show Activity2 from Fragment1 of MainActivity and get the items to the fragment1 when clicking "Select" button of Activity2?

  1. Want to show Activity2 from Fragment1(MainActivity)
  2. Want to get the selected items from Activity2 to Fragment1

Picture A


Solution

  • If I understood your question correctly, it seems like what you want to do can be achieved with startActivityForResult() but it will be soon deprecated.

    So I would suggest to use ActivityResultLauncher.

    You can read more about it here OnActivityResult method is deprecated, what is the alternative?