Android重要组成单元:Activity、Service、BroadcastReceiver、ContentProvider
实际应用中往往包括多个Activity,不同Activity向用户呈现不同的界面操作。
Android应用的多个Activity组成Activity栈,当前活动的Activity位于栈顶
4.1 建立、配置和使用Activity
Activity间接或直接地继承了Context、ContextWrapper、ContextThemeWrapper等基类,因此可以直接调用它们的方法
配置Activity
Android应用要求所有应用程序组件(Activity、Service、ContentProvider、BroadcastReceiver)都必须显式进行配置
只需添加<activity …/>的子元素即可
name
icon
label
exported:指定该Activity是否允许被其他应用调用
launchMode:指定加载模式,支持standard、singleTop、singleTask和singleInstance这4种模式
此外,通常还会添加一个或多个<intent-filter.../>元素,该元素用于指定该Activity可响应的Intent
4.1.3 启动、关闭Activity
虽然程序可能包含多个Activity,但只有一个Activity会作为程序的入口。
Activity启动其他Activity有如下两个方法:
startActivity(Intent intent):启动其他Activity
startActivityForResult(Intent intent, int requestCode):以指定的请求码启动Activity。
Intent是Android应用个组件之间通信的重要方式,可以用来启动Activity,也可以用来启动Service
Activity关闭有如下方法:
finish():Activity
finishActivity(int requestCode):
示例程序代码:
public
class
MainActivity
extends
ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button bn = (Button) findViewById(R.id. bn );
bn.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View source)
{
Intent intent = new Intent(MainActivity. this , SecondActivity. class );
startActivity(intent);
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button bn = (Button) findViewById(R.id. bn );
bn.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View source)
{
Intent intent = new Intent(MainActivity. this , SecondActivity. class );
startActivity(intent);
}
});
}
}
第二个Activity代码:
public
class
SecondActivity
extends
Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout. second );
Button previous = (Button) findViewById(R.id. previous );
Button close = (Button) findViewById(R.id. close );
previous.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View source)
{
Intent intent = new Intent(SecondActivity. this ,
MainActivity. class );
startActivity(intent);
}
});
close.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View source)
{
Intent intent = new Intent(SecondActivity. this ,
MainActivity. class );
startActivity(intent);
finish();
}
});
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout. second );
Button previous = (Button) findViewById(R.id. previous );
Button close = (Button) findViewById(R.id. close );
previous.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View source)
{
Intent intent = new Intent(SecondActivity. this ,
MainActivity. class );
startActivity(intent);
}
});
close.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View source)
{
Intent intent = new Intent(SecondActivity. this ,
MainActivity. class );
startActivity(intent);
finish();
}
});
}
}
当然,不可忘记在Android manifest XML文件中添加Activity定义
4.1.4 使用Bundle在Activity之间交换数据
两个Activity之间有一个信使Intent,我们只要将需要交换的数据放入Intent即可。Intent提供了多个方法来携带额外数据:
putExtras(Bundle data)
| 向Intent中放入需要携带的数据包 |
Bundle getExtras()
|
取出Intent所携带的数据包
|
putExtras(String name, Xxx value)
|
向Intent中按key-value对的形式存入数据
|
getXxxExtras(String name)
|
从Intent中按key取出指定类型的数据
|
上面的Bundle就是一个简单的数据携带包,该Bundle对象包含了多个方法来存入数据:
- putXxx(String key, Xxx data):向Bundle放入Int、Long等各种类型数据
- putSerializable(String key, Serializable data):从Bundle取出一个可序列化的对象
示例代码:
main.java
public
class
MainActivity
extends
ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button bn = (Button) findViewById(R.id. bn );
bn.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
EditText name = (EditText) findViewById(R.id. name );
EditText passwd = (EditText) findViewById(R.id. password );
RadioButton male = (RadioButton) findViewById(R.id. male );
// RadioButton female = (RadioButton) findViewById(R.id.female);
String gender = male.isChecked()? "male" : "female" ;
Person p = new Person(name.getText().toString(), passwd.getText().toString(), gender);
Bundle data = new Bundle();
data.putSerializable( "person" , p);
Intent intent = new Intent(MainActivity. this , ResultActivity. class );
intent.putExtras(data);
startActivity(intent);
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
Button bn = (Button) findViewById(R.id. bn );
bn.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
EditText name = (EditText) findViewById(R.id. name );
EditText passwd = (EditText) findViewById(R.id. password );
RadioButton male = (RadioButton) findViewById(R.id. male );
// RadioButton female = (RadioButton) findViewById(R.id.female);
String gender = male.isChecked()? "male" : "female" ;
Person p = new Person(name.getText().toString(), passwd.getText().toString(), gender);
Bundle data = new Bundle();
data.putSerializable( "person" , p);
Intent intent = new Intent(MainActivity. this , ResultActivity. class );
intent.putExtras(data);
startActivity(intent);
}
});
}
}
ResultActivity.java————
public
class
ResultActivity
extends
Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. result );
TextView name = (TextView) findViewById(R.id. name );
TextView passwd = (TextView) findViewById(R.id. password );
TextView gender = (TextView) findViewById(R.id. gender );
Intent intent = getIntent();
Person p = (Person) intent.getSerializableExtra( "person" );
name.setText( "your acount name :" + p.getName());
passwd.setText( "your password :" + p.getPass());
gender.setText( "your gender :" + p.getGender());
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. result );
TextView name = (TextView) findViewById(R.id. name );
TextView passwd = (TextView) findViewById(R.id. password );
TextView gender = (TextView) findViewById(R.id. gender );
Intent intent = getIntent();
Person p = (Person) intent.getSerializableExtra( "person" );
name.setText( "your acount name :" + p.getName());
passwd.setText( "your password :" + p.getPass());
gender.setText( "your gender :" + p.getGender());
}
}
main Activity布局文件:
<
TableLayout
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" >
< TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "please input your register information"
android:textSize = "20sp" />
< TableRow >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "user name"
android:textSize = "20sp" />
< EditText
android:id = "@+id/name"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint = "enter your account"
android:selectAllOnFocus = "true" />
</ TableRow >
< TableRow >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "code:"
android:textSize = "16sp" />
< EditText
android:id = "@+id/password"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:password = "true"
android:selectAllOnFocus = "true" />
</ TableRow >
< TableRow >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "gender:"
android:textSize = "16sp" />
< RadioGroup
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal" >
< RadioButton
android:id = "@+id/male"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "male"
android:textSize = "16sp" />
< RadioButton
android:id = "@+id/female"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "female"
android:textSize = "16sp" />
</ RadioGroup >
</ TableRow >
< Button
android:id = "@+id/bn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "register"
android:textSize = "16sp" />
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical" >
< TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "please input your register information"
android:textSize = "20sp" />
< TableRow >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "user name"
android:textSize = "20sp" />
< EditText
android:id = "@+id/name"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint = "enter your account"
android:selectAllOnFocus = "true" />
</ TableRow >
< TableRow >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "code:"
android:textSize = "16sp" />
< EditText
android:id = "@+id/password"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:password = "true"
android:selectAllOnFocus = "true" />
</ TableRow >
< TableRow >
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:text = "gender:"
android:textSize = "16sp" />
< RadioGroup
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal" >
< RadioButton
android:id = "@+id/male"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "male"
android:textSize = "16sp" />
< RadioButton
android:id = "@+id/female"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "female"
android:textSize = "16sp" />
</ RadioGroup >
</ TableRow >
< Button
android:id = "@+id/bn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "register"
android:textSize = "16sp" />
</
TableLayout
>
4.1.5 启动其他Activity并返回结果
Activity还提供了一个startActivityForResult(Intent intent, int requestCode)
这种启动其他Activity并返回结果的场景也是通过Bundle进行数据交换的,为了获取Activity所返回的结果,需要从两方面着手:
- 当前Activity需要重写onActivityResult(int requestCode, int resultCode, Intent intent),当被启动Activity返回结果时该方法会被触发。resultCode代表Activity返回的结果码,这个结果码由开发者根据业务自行设定
- 被启动Activity需要调用setResult()方法设置处理结果