activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<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:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Text Me" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" android:text="Send" /> </LinearLayout> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.phaisarn.myapplication import android.app.Activity import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.EditText import android.widget.Toast const val EXTRA_MESSAGE1 = "com.phaisarn.myapplication.MESSAGE1" const val EXTRA_MESSAGE2 = "com.phaisarn.myapplication.MESSAGE2" const val CODE = 1 class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } /** Called when the user taps the Send button */ fun sendMessage(view: View) { val editText = findViewById<EditText>(R.id.editText) val message = editText.text.toString() val intent = Intent(this, SecondActivity::class.java).apply { putExtra(EXTRA_MESSAGE1, message) } startActivityForResult(intent, CODE) } override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent) { if (requestCode == CODE) { if (resultCode == Activity.RESULT_OK) { val msg = intent.getStringExtra(EXTRA_MESSAGE2) Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() } } } } |
activity_second.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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:orientation="horizontal" tools:context=".SecondActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Return main Activity" /> </LinearLayout> |
SecondActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.phaisarn.myapplication import android.app.Activity import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.TextView import android.content.Intent import android.view.View import android.widget.EditText class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) // Get the Intent that started this activity and extract the string val message = intent.getStringExtra(EXTRA_MESSAGE1) // Capture the layout's TextView and set the string as its text val textView = findViewById<TextView>(R.id.textView).apply { text = message } } fun onClick(view: View) { val editText = findViewById<EditText>(R.id.editText) val msg = editText.text.toString() val intent = Intent() intent.putExtra(EXTRA_MESSAGE2, msg) setResult(Activity.RESULT_OK, intent) finish() } } |
บรรทัดที่ 34 : ปิด activity นี้ ด้วย finish()
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.phaisarn.myapplication"> <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" android:parentActivityName=".MainActivity"></activity> </application> </manifest> |