This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Tuesday, March 17, 2020

How to Pass Data from One Activity to Another activity in kotlin Android


       How to Pass Data from One Activity to Another activity in kotlin Android 


How to Pass Data from One Activity to Another Activity in kotlin Android


In this tutorial, you will learn to pass data from one activity to another Activity  in kotlin android with and without using intent.

1.    Using Intent
2.    Using Global Variables

How to Pass Data from One Activity to Another in Android

Sending Data:


val intent = Intent(context, DestinationActivityName::class.java)
intent.putExtra(Key, Value)
startActivity(intent)

Here I am sending just one value, in the same way, you can attach more values by using putExtra() method.


Retrieving Data:


var intent = getIntent()
var str = intent.getStringExtra(Key)


Full Example  How to Pass Data from One Activity to Another in kotlin Android


1.MainActivity.kt:

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText

class First:AppCompatActivity() {
  internal var textBox:EditText
  internal var passButton:Button

  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_first)

    textBox = findViewById(R.id.textBox) as EditText
    passButton = findViewById(R.id.passButton) as Button

    passButton.setOnClickListener(object:View.OnClickListener() {
      fun onClick(v:View) {
        val str = textBox.getText().toString()
        val intent = Intent(getApplicationContext(), Second::class.java)
        intent.putExtra("message", str)
        startActivity(intent)
      }
    })
  }
}




2.activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="15dp"
    tools:context="com.androidexample.First"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textBox"
        android:hint="Enter Your Message"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/passButton"
        android:text="Pass"/>
</LinearLayout>






3.Second_Activity.kt:

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class Second_Activity:AppCompatActivity() {
  internal var text:TextView

  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_second)
    text = findViewById(R.id.text) as TextView

    val intent = getIntent()
    val str = intent.getStringExtra("message")
    text.setText(str)
  }
}


4.activity_second:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="15dp"
    tools:context="com.androidexample.Second">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text"/>
</LinearLayout>


5.manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.javatpoint.hello" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity 
            android:name=".MainActivity" 
            android:label="@string/title_activity_main" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 

        <activity 
            android:name=". Second_Activity
            android:label="@string/title_activity_main" /> 

    </application> 
 
</manifest> 





No comments:

Post a Comment