Skip to main content
ubuntuask.com

Back to all posts

How to Send Data Another Page And List With Kotlin?

Published on
3 min read
How to Send Data Another Page And List With Kotlin? image

Best Kotlin Resources to Buy in October 2025

1 Functional Programming in Kotlin by Tutorials (First Edition): A Practical Approach to Writing Safer, More Reliable Apps

Functional Programming in Kotlin by Tutorials (First Edition): A Practical Approach to Writing Safer, More Reliable Apps

BUY & SAVE
$59.99
Functional Programming in Kotlin by Tutorials (First Edition): A Practical Approach to Writing Safer, More Reliable Apps
2 Kotlin Coroutines by Tutorials (Third Edition): Best Practices to Create Safe & Performant Asynchronous Code With Coroutines

Kotlin Coroutines by Tutorials (Third Edition): Best Practices to Create Safe & Performant Asynchronous Code With Coroutines

BUY & SAVE
$59.99
Kotlin Coroutines by Tutorials (Third Edition): Best Practices to Create Safe & Performant Asynchronous Code With Coroutines
3 Kotlin Apprentice (Third Edition): Beginning Programming with Kotlin

Kotlin Apprentice (Third Edition): Beginning Programming with Kotlin

BUY & SAVE
$59.99
Kotlin Apprentice (Third Edition): Beginning Programming with Kotlin
4 Data Structures & Algorithms in Kotlin (Second Edition): Implementing Practical Data Structures in Kotlin

Data Structures & Algorithms in Kotlin (Second Edition): Implementing Practical Data Structures in Kotlin

BUY & SAVE
$59.99
Data Structures & Algorithms in Kotlin (Second Edition): Implementing Practical Data Structures in Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Reactive Programming with Kotlin (Second Edition): Learn RX with RxJava, RxKotlin and RxAndroid

Reactive Programming with Kotlin (Second Edition): Learn RX with RxJava, RxKotlin and RxAndroid

BUY & SAVE
$59.99
Reactive Programming with Kotlin (Second Edition): Learn RX with RxJava, RxKotlin and RxAndroid
7 Kotlin in Action

Kotlin in Action

BUY & SAVE
$34.71 $44.99
Save 23%
Kotlin in Action
8 Android Fundamentals by Tutorials (First Edition): Build Android Apps With Kotlin & Jetpack Compose

Android Fundamentals by Tutorials (First Edition): Build Android Apps With Kotlin & Jetpack Compose

BUY & SAVE
$59.99
Android Fundamentals by Tutorials (First Edition): Build Android Apps With Kotlin & Jetpack Compose
9 Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

BUY & SAVE
$50.99
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)
+
ONE MORE?

To send data to another page and list with Kotlin, you can use intents to pass information between activities. You can put extra data in the intent using key-value pairs and retrieve the data on the receiving page. To display a list of data, you can use RecyclerView to populate a list view with the data from a data source, such as a list or array. You can customize the layout of each list item using a custom adapter.

What is the use of bundles in Kotlin?

In Kotlin, bundles are used to pass data between activities or fragments. Bundles are collections of key-value pairs, where the keys are strings and the values can be various data types such as strings, integers, booleans, etc.

Bundles are commonly used when starting a new activity or fragment, allowing data to be passed along with the intent. This can be used to transfer information needed by the new activity or fragment to display or process.

For example, you can put data into a bundle before starting a new activity:

val intent = Intent(this, SecondActivity::class.java) val bundle = Bundle() bundle.putString("key", "value") intent.putExtras(bundle) startActivity(intent)

Then, in the receiving activity, you can retrieve the data from the bundle:

val bundle = intent.extras val value = bundle?.getString("key")

How to use the putExtra method to send data in Kotlin?

In Kotlin, you can use the putExtra method to add extra data to an Intent before starting a new activity. Here's an example of how to use putExtra to send data:

  1. Create an Intent to start the new activity:

val intent = Intent(this, SecondActivity::class.java)

  1. Use the putExtra method to add extra data to the Intent:

intent.putExtra("key", "value")

In this example, "key" is a string identifier for the data and "value" is the data itself.

  1. Start the new activity with the Intent:

startActivity(intent)

  1. In the receiving activity, extract the data using the same key:

val data = intent.getStringExtra("key")

That's it! You have successfully sent and received data using the putExtra method in Kotlin.

How to send data to a fragment in Kotlin?

There are several ways to send data to a fragment in Kotlin. Some common methods include using arguments, using a ViewModel, and using the Fragment's own methods to set data.

  1. Using arguments: You can pass data to a fragment by setting arguments on the fragment before adding it to the activity. Here's how you can do it:

val fragment = MyFragment() val bundle = Bundle() bundle.putString("key", "value") fragment.arguments = bundle

supportFragmentManager.beginTransaction() .replace(R.id.container, fragment) .commit()

Inside the fragment, you can retrieve the data using arguments:

val data = arguments?.getString("key")

  1. Using ViewModel: You can use a ViewModel to share data between fragments and activities. Create a ViewModel class that holds the data and retrieve it in the fragment using ViewModelProviders:

class MyViewModel : ViewModel() { var data: String = "" }

// In the fragment val viewModel = ViewModelProviders.of(activity).get(MyViewModel::class.java) val data = viewModel.data

  1. Using Fragment's own methods: You can also create setter methods in the fragment to set data:

class MyFragment : Fragment() { var data: String = ""

fun setData(data: String) {
    this.data = data
}

}

// In the activity val fragment = MyFragment() fragment.setData("value")

These are just a few ways to send data to a fragment in Kotlin. Choose the method that best suits your use case.