In this article today learn kotlin intent example. For android
studio. Intent as using a one activity to another activity so follow the source
code kotlin intent example.
Android Intent is an informing object used to demand another
application part to play out an activity. Intent encourages clients to speak
with application segment through a few different ways, for example, beginning
an activity, beginning a help, conveying a communicate recipient, and so on.
Intents (android.content.Intent) are the informing framework
by which one activity can dispatch another activity. An activity can, for
example, issue an intent to demand the dispatch of another activity contained
inside a similar application. Intents additionally, be that as it may, go past
this idea by permitting an activity to demand the administrations of some other
suitably enrolled activity on the gadget for which authorizations are arranged.
Consider, for example, an activity contained inside an application that
requires a website page to be stacked and shown to the client. Instead of the
application containing a subsequent activity to play out this assignment, the
code can basically send an intent to the Android runtime mentioning the
administrations of any activity that has enlisted the capacity to show a site
page. The runtime framework will coordinate the solicitation to accessible
exercises on the gadget and either dispatch the activity that matches or, in
case of numerous matches, permit the client to choose which activity to
utilize.
Intents likewise take into account the exchange of information
from the sending activity to the getting activity. In the recently illustrated
situation, for example, the sending activity would need to send the URL of the
website page to be shown to the subsequent activity. Additionally, the
accepting activity may likewise be arranged to return information to the
sending activity when the necessary undertakings are finished.
Follow the kotlin intent example source code.
1.MainActivity.kt:
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
val id:Int = 10
val language:String = "kotlin"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener() {
intent = Intent(this, SecondActivity::class.java)
intent.putExtra("id_value", id)
intent.putExtra("language_value", language)
startActivity(intent)
}
}
}
2.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="example.javatpoint.com.kotlinexplicitintent.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="First Activity"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.172" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.77" />
</android.support.constraint.ConstraintLayout>
3.SecondActivity.kt
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_second.*
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val bundle:Bundle = intent.extras
val id = bundle.get("id_value")
val language = bundle.get("language_value")
Toast.makeText(applicationContext,id.toString()+" "+language,Toast.LENGTH_LONG).show()
button2.setOnClickListener(){
intent = Intent(this,MainActivity::class.java)
startActivity(intent)
}
}
}
4.activity_second:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="example.javatpoint.com.kotlinexplicitintent.SecondActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="SecondActivity"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.107" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
</android.support.constraint.ConstraintLayout>
5.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.spinnersqlite">
<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/AppTheme">
<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>
Comments
Post a Comment