Data Sharing between Activities using putExtra Intent in Android Studio Using KOTLIN

Introduction

Sometimes one Activity depends on the data provided by the previous Activity, for example, a Product Display Page would require a product ID that the user has selected from the previous Activity’s ListView. There are several ways to do this, and one of them is using putExtra in Intent. PutExtra can hold data with a TAG that acts as an identifier, this data then can be accessed from the next activity using getExtra method.

Setup Activities

Open Project in Android Studio and select Kotlin as language. Once Gradle is finished loading, create a new Kotlin Class and a new XML layout. Open Second Activity and attach the XML layout in OnCreate method.

In this tutorial, SecondActivity is used as the name of Second Activity, and second_activity is used as the name of Second Activity’s XML layout file name.

Below is the code for attaching XML in second activity.


class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.second_activity)
    }
}

Now, Open AndroidManifest and register your second activity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.basiccodist.datashareintent_master">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DataShareIntentMaster">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
</application>

</manifest>

Design Your Layout

Open activity_main.xml and paste the code given below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="125dp"
android:layout_height="48dp"
android:text="Click Here"
tools:layout_editor_absoluteX="143dp"
tools:layout_editor_absoluteY="341dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

This will add a Button with text “Click Here”. It will be used to move to the next activity.

Now, let’s design our second activity layout, open second activity xml file, in this case second_activity.xml

Paste the code given below

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
tools:layout_editor_absoluteX="199dp"
tools:layout_editor_absoluteY="357dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

This will a TextView to your second activity which will display the message from first activity.

MainActivity

Open MainActivity and paste the code given below

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val m_button = findViewById<Button>(R.id.button) as Button
m_button.setOnClickListener{
val intent = Intent(this,SecondActivity::class.java)
intent.putExtra("message","Hello! This data came from first activity.")
startActivity(intent)
}
}
}

Here, m_button is the reference for the button on the first activity XML. m_button.setOnClickListener will help us to register the function that needs to performed when button is pressed.

intent is a variable that holds the next activity name, using intent.putExtra, we add extra information that will be carried to the next activity. startActivity(intent) will start the next assigned activity.

Second Activity

Open Second Activity class and place the code given below


class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second_activity)

val m_message = intent.getStringExtra("message")
val messageText = findViewById<TextView>(R.id.textView) as TextView
messageText.text = m_message
}
}

Here, intent.getStringExtra(“message”) will fetch data which was transferred from previous activity with a tag.

Once fetched, we are assigning the TextView and loading the message.

Save and Run.

Where To Go From Here?

There are other ways to transfer data from one activity to other, one of them is using ViewModel. Try using ViewModel in your project.

Written by

XR Developer responsible for end-to-end development of XR solutions spanning multiple domains, by using various XR and WebXR libraries.

3 thoughts on “Data Sharing between Activities using putExtra Intent in Android Studio Using KOTLIN

  1. Hey I did the same thing but when I run my app and click the button in the main activity the app crashes instead of creating second activity. Can you think of any reason why? Also your video doesn’t have audio. Great website btw.

    1. Hi, thank you so much for visiting. Have you registered your second activity in the AndroidManifest.xml
      I would suggest to you download the source code from GitHub and try comparing the scripts. If the problem still exists, let me know, I contact you on discord to resolve this issue.

Leave a Reply