Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
www . i t s c i .m j u . ac . t h / sayan
MULTIPLE ACTIVITIES AND INTENTS
SAYAN UNANKARD
3
ACTIVITY LIFECYCLE
2
ACTIVITY STATE TRANSITIONS
กรณีที่ App มีหลาย Activities
• เรียกหน8าจออ่ืน ๆ ใน App เดียวกัน จะอยูCในสถานะ OnPause/Resume
• เรียกหน8าจออ่ืน ๆ ตCาง App จะอยูCในสถานะ onStop/Start
3
Activity1 Activity2
Activity1: OnPause()
Activity1: OnResume()
Activity1 Activity1
Activity1: OnStop()
Activity1: OnStart()
App1 App2
MULTIPLE ACTIVITIES
App สCวนใหญCมีมากกวCา 1 Activity โดยสCวนมากจะพบในลักษณะของ List กับ Details เชCน
• Activity1 สามารถสCงข8อมูลไปให8 Activity2 ได8
• Activity2 สามารถสCงข8อมูลกลับมายัง Activity1 ได8เมื่อประมวลผลเสร็จแล8ว
4
Activity1 Activity2
List Details
ADDING NEW ACTIVITY
ใน Android Studio คลิกขวาที่ package ที่ต8องการสร8าง เลือก New -> Activity -> Empty Activity โดยจะได8ส่ิงเพิ่มเข8ามาในโปรเจ็คดังนี้
• layout.xml
• activity.java
• AndroidManifest.xml
5
ANDROIDMANIFEST.XML
6
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sayan.lab03">
<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".PlayGameActivity"></activity><activity android:name=".MainActivity">
<intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />
</intent-filter></activity>
</application>
</manifest>สำหรับ run เปDนหนFาแรก
INTENTS
Intent คือ Object ที่เปzนตัวแทนของ action ที่ต8องการให8 App ทำงาน โดยใช8เปzนตัวเชื่อมระหวCาง Activity
• สามารถเชื่อมตCอระหวCาง activity ใน App เดียวกันหรือตCาง App ก็ได8
• สามารถเก็บข8อมูลในรูปแบบของ Parameter ที่สCงไปยัง Activity ที่ต8องการเรียกใช8ได8
• Activity ที่ถูกเรียกใช8 สามารถ return ข8อมูลกลับมายัง Activity ที่เรียกใช8งานได8
7
Activity1 Activity2intent
USES OF INTENTS
• ใช8สำหรับ start activity จากอีก activity หนึ่ง
• ใช8สำหรับ start service เชCน camera, phone dialer, music player,…
• ใช8สำหรับ broadcast a message ไปยัง app หรือ service อ่ืน ๆ
Intent แบCงเปzน 2 ประเภท คือ
• Explicit โดยการระบุชื่อ activity ที่ต8องการเรียกใช8 เชCน run Activity2
• Implicit โดยการอธิบายงานที่ต8องการให8ทำงาน เชCน launch web browser ซึ่งมีหลาย App
ที่สามารถจัดการการทำงานนี้ได8
8
การสรFาง INTENT
• ในกรณีที่ต8องการให8เรียกใช8 Activity อ่ืน ๆ สามารถทำได8โดยการ new Intent() จากนั้นเรียกใช8 method startActivity() เชCน
Intent intent = new Intent(this, PlayGameActivity.class);
startActivity(intent);
• กรณีที่ต8องการสCงข8อมูลไปด8วย สามารถสCง parameter ได8ดังนี้
Intent intent = new Intent(this, PlayGameActivity.class);
intent.putExtra("name", value);
intent.putExtra("name", value);
startActivity(intent);
โดยที่ Method putExtra เก็บข8อมูลในรูปแบบของ key/value
9
การอLานคLา EXTRA
ในกรณีที่มีการสCง parameter มาจาก Activity1 มายัง Activity2 ใน Activity2 สามารถอCานคCา Extra data ได8โดยการเรียกใช8 Method getIntent(); จากนั้นเรียก getXXXExtra(“name”); ตาม
ชนิดของข8อมูลที่สCงเข8ามา เชCน getIntExtra(“…”) หรือ getStringExtra(“…”);
10
Activity1 Activity2Intent
(extra data)
Intent intent = new Intent(this,Activity2.class);intent.putExtra("id", "1001");intent.putExtra("name", "Sayan");startActivity(intent);
Intent intent = getIntent();int id = intent.getIntExtra("id");String name = intent.getStringExtra("name");
กรณีท่ีตFองการรอคืนคLา
ในกรณีที่ Activity1 เรียกใช8 Activity2 แล8วต8องการรอให8 Activity2 คืนคCากลับมานั้น จะต8องใช8คำส่ัง startActivityForResult() แทนคำส่ัง startActivity()
• คำส่ัง startActivityForResult() ต8องการให8ระบุ unique ID เพื่อระบุวCามาจากการเรียกใช8
Activity ใด
• เขียน Method onActivityResult() ซึ่งจะถูกเรียกใช8งานเมื่อ Activity2 ทำงานเสร็จ
เรียบร8อย โดยการเรียกคCา unique ID ที่สCงไปผCาน Method startActivityForResult()
• เขียนโค8ดในสCวนของ Activity2 เพื่อสCงคCากลับมาโดยใช8คำส่ัง setResult()
11
ตัวอยLางการคืนคLา
12
Activity1 Activity2
1) onClick()2) new Intent()3) startActivityForResult(intent, req_code)8) onActivityResult()
4) onClick()5) intent.putExtra(…)6) setResult(RESULT_OK, intent)7) finish()
Intent(extra data)
ตัวอยLางการคืนคLา
13
Activity1 Activity2Intent
private static final int REQ_CODE = 123; //0- 65535
public void onClick(View view) {Intent intent = new Intent(this, Activity2.class);startActivityForResult(intent, REQ_CODE);
}
public void onClick(View view) {EditText name = (EditText)
findViewById(R.id.txtname);
Intent intent = new Intent();intent.putExtra("fullname",
name.getText().toString());setResult(RESULT_OK, intent);finish();
}protected void onActivityResult(int requestCode,
int resultCode, Intent data) {super.onActivityResult(requestCode,
resultCode, data);if (requestCode == REQ_CODE &&
resultCode == RESULT_OK) {fullname = data.getStringExtra("fullname");
}}
ตัวอยLางการคืนคLา
14
Sayan
MAINACTIVITY.JAVA
15
private static final int REQ_CODE = 123; // 0-65535private String fullname = "";
...
public void onAddPlayerClick(View view) {Intent intent = new Intent(this,AddPlayerActivity.class);startActivityForResult(intent,REQ_CODE);
}
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQ_CODE && resultCode == RESULT_OK) {
fullname = data.getStringExtra("fullname");}
}
public void onPlayClick(View view) {Intent intent = new Intent(this,PlayGameActivity.class);intent.putExtra("name",fullname);startActivity(intent);
}
ADDPLAYERACTIVITY.JAVA
16
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_player);
}
public void addPlayerClick(View view) {EditText name = (EditText) findViewById(R.id.txtname);
Intent intent = new Intent();intent.putExtra("fullname", name.getText().toString());setResult(RESULT_OK, intent);finish();
}
PLAYGAMEACTIVITY.JAVA
17
private String name = "";...
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_player);
...Intent intent = getIntent();name = intent.getStringExtra("name");
TextView txtscore = (TextView) findViewById(R.id.score);txtscore.setText(name + ", you got score : " + score);
...}
PASS THE OBJECT WITH INTENT (SERIALIZABLE)
กำหนด Class ให8 implements Serializable
18
import java.io.Serializable;
public class Student implements Serializable {private String stuid;private String stuname;private double gpa;
public Student() {}
public Student(String stuid, String stuname, double gpa) {this.stuid = stuid;this.stuname = stuname;this.gpa = gpa;
}……
}
PASS THE OBJECT WITH INTENT (SERIALIZABLE)
เรียกใช8 intent.putExtra() โดยสCง Object s ไป
รับคCาใน SecondActivity โดยใช8 method getSerializableExtra()
19
public void onBtnClick(View view) {Student s = new Student("001", "Sayan", 3.56);Intent intent = new Intent(this, SecondActivity.class);intent.putExtra("student", s);startActivity(intent);
}
Student s = (Student) getIntent().getSerializableExtra("student");Toast.makeText(this, "student = " + s.getStuname(),
Toast.LENGTH_SHORT).show();
PASS THE OBJECT WITH INTENT (PARCELABLE)
กำหนด Class ให8 implements Parcelable
20
import android.os.Parcel;import android.os.Parcelable;public class Student implements Parcelable {
private String stuid;private String stuname;private double gpa;
public static final Creator<Student> CREATOR = new Creator<Student>() {@Overridepublic Student createFromParcel(Parcel in) {
return new Student(in);}@Overridepublic Student[] newArray(int size) {
return new Student[size];}
};
PASS THE OBJECT WITH INTENT (PARCELABLE)
เพิ่ม Method สำหรับจัดการ Parcel ในคลาส
21
public Student(Parcel in) {this.stuid = in.readString();this.stuname = in.readString();this.gpa = in.readDouble();
}
@Overridepublic int describeContents() {
return 0;}
@Overridepublic void writeToParcel(Parcel dest, int flags) {
dest.writeString(stuid);dest.writeString(stuname);dest.writeDouble(gpa);
}
PASS THE OBJECT WITH INTENT (PARCELABLE)
เรียกใช8 intent.putExtra() โดยสCง Object s ไป
รับคCาใน SecondActivity โดยใช8 method getSerializableExtra()
22
public void onBtnClick(View view) {Student s = new Student("001", "Sayan", 3.56);Intent intent = new Intent(this, SecondActivity.class);intent.putExtra("student", s);startActivity(intent);
}
Student s = (Student) getIntent().getParcelableExtra("student");Toast.makeText(this, "student = " + s.getStuname(),
Toast.LENGTH_SHORT).show();
IMPLICIT INTENT
ใช8ในกรณีที่ต8องการรัน App อ่ืน ๆ โดยไมCได8เจาะจงชื่อ App เชCน invoke default browser หรือ load music player to play a song
Make a phone call
23
Uri number = Uri.parse("tel:053873900");Intent intent = new Intent(Intent.ACTION_DIAL, number);startActivity(intent);
IMPLICIT INTENT
Goto web page
Open a map pointing at latitude and longitude (z = zoom)
24
Uri webpage = Uri.parse("http://www.itsci.mju.ac.th");Intent intent = new Intent(Intent.ACTION_VIEW, webpage);startActivity(intent);
Uri location = Uri.parse("geo:18.8988921,99.0111493?z=14");Intent intent = new Intent(Intent.ACTION_VIEW, location);startActivity(intent);