0

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

마카롱
  • 5
  • 1
  • Perhaps you should not have separate activities, but rather a single activity. Android app development is strongly moving in the direction of having few activities, perhaps just one, for the entire app. If you used a single activity here, your two fragments could share a `ViewModel` instance, and the "select" fragment could call a function on the shared `ViewModel` to indicate what the selections are. – CommonsWare Sep 18 '21 at 11:37
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 23 '21 at 08:41

3 Answers3

1

For start another activity action this like below:

with parameters:

     val intent = Intent(this, ExaplesActivity::class.java).apply {
        putExtra(MESSAGE, message)
    }
    startActivity(intent)

OR without parameter :

     val intent = Intent(this, NextActivity::class.java)
 startActivity(intent)
0

use this code to start next activity

startActivity(Intent(requiredContext(), SecondActivity::class.java))
Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31
0

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?

Void
  • 1,129
  • 1
  • 8
  • 21