Skip to content

Instantly share code, notes, and snippets.

@Pornpun
Created September 19, 2015 12:56
Show Gist options
  • Save Pornpun/7b8228ab5005547f7d46 to your computer and use it in GitHub Desktop.
Save Pornpun/7b8228ab5005547f7d46 to your computer and use it in GitHub Desktop.
ItentTest
<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="com.mystou.intenttest.DetailActivity">
<TextView
android:text="@string/page_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ffff3131"/>
<Button
android:id="@+id/btn_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="goToHome"
android:text="@string/btn_goto_home"
android:layout_marginTop="100dp"/>
</LinearLayout>
<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">
<TextView
android:text="@string/page_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#ff0944ff"/>
<Button
android:id="@+id/btn_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="goToDetail"
android:text="@string/btn_goto_detail"
android:layout_marginTop="100dp"/>
</LinearLayout>
package com.mystou.intenttest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DetailActivity extends Activity {
private Button btnGoHome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// เชื่อมต่อกับ xml
matchView();
}
private void matchView() {
btnGoHome = (Button) findViewById(R.id.btn_detail);
}
public void goToHome(View view) {
finish(); // สั่งปิดหน้าปัจจุบัน
// ถ้าหน้า MainActivity.java สั่ง finish();
// Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// startActivity(intent);
// finish();
}
}
package com.mystou.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnGoDetail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// เชื่อมต่อกับไฟล์ xml
matchView();
}
private void matchView() {
btnGoDetail = (Button) findViewById(R.id.btn_home);
}
// ทำงานเมื่อปุ่มถูกกด
public void goToDetail(View view) {
// สร้าง instance คลาส Intent
// พร้อมกับส่ง argument ไป 2 ค่า คือ
// 1. MainActivity.this หรือใช้คำสั่ง getApplicationContext() Activity ปัจจุบัน
// 2. DetailActivity.class เป็น Activity ใหม่ที่จะเปิด
// หรือ MainActivity.this
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
startActivity(intent); // สั่ง Activity ให้เริ่มต้นทำงาน
//finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">IntentTest</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="page_home">Page : Home</string>
<string name="page_detail">Page : รายละเอียด</string>
<string name="btn_goto_detail">รายละเอียด</string>
<string name="btn_goto_home">Home</string>
<string name="title_activity_detail">DetailActivity</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment