我们知道手机软件不可能只有一个界面,一般软件我们点击确定、前进等按钮都会跳转到一个新的界面,今天主要谈谈Activity的跳转,即从原来的Activity跳到新的Activtiy。上一篇博文主要谈了Activity的定义和生命周期,不过谈得比较抽象,今天结合代码来实例展示Activity的生命周期。
startActivity(Intent intent),关于这个方法,开发文档的解释如下:
Launch a new activity. You will not receive any information about when the activity exits. This implementation overrides the base version, providing information about the activity performing the launch. Because of this additional information, the FLAG_ACTIVITY_NEW_TASK
launch flag is not required; if not specified, the new activity will be added to the task of the caller.
This method throws ActivityNotFoundException
参数
intent | The intent to start. |
大概意思就是打开一个新的Activity,这里的关键点是information,文档说information是从那个触发跳转的Activity来的那怎么来的?显而易见,应该就是那个参数Intent对象了。什么是Intent呢?万能的开放文档告诉我们:
An intent is an abstract description of an operation to be performed. It can be used with startActivity
to launch an Activity
, broadcastIntent
to send it to any interested BroadcastReceiver
components, and startService(Intent)
or bindService(Intent, ServiceConnection, int)
to communicate with a background Service
.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.
简单来说Intent是一个用于Activity、BroadcastReceiver、Service等组件的通信的行为的抽象的描述,啥意思?最后一句说它如同把两个Activity连接的胶水比较形象。Intent这个单词的意思是意图,所以可以理解为一个Activity要打开另一个Activity的时候,它会携带一些意图,让新的Activity接收,让新的Activity知道它应该做什么事。就像你叫一个人过来,你总有意图,比如跟他说“今晚一起去约会”什么的。
为了展示打开新的Activity这一过程,我使用一个实际例子来说下。这是一个非常简单的乘法计算器功能的应用在第一个界面输入要计算的数值,在第二个界面显示计算结果。以下是代码:
第一个界面(MainActivity):
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private EditText factorOne ;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据控件的ID来取得代表控件的对象
factorOne = (EditText)findViewById(R.id.factorOne);
factorTwo = (EditText)findViewById(R.id.factorTwo);
symbol = (TextView)findViewById(R.id.symbol);
calculate = (Button)findViewById(R.id.calculate);
//为symbol和calculate设置显示的值
// symbol.setText("乘以");
// calculate.setText("计算");
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
//将监听器的对象绑定到按钮对象上面
calculate.setOnClickListener(new CalculateListener());
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("MainActivity--->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("MainActivity--->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("MainActivity--->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("MainActivity--->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("MainActivity--->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("MainActivity--->onStop");
super.onStop();
}
class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) {
//取得两个EditText控件的值
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
//跳转的关键代码将这两个值以键值对的形式存放到Intent对象当中
Intent intent = new Intent();
intent.putExtra("one",factorOneStr);
intent.putExtra("two",factorTwoStr);
intent.setClass(MainActivity.this, ResultActivity.class);
//使用这个Intent对象来启动ResultActivity
MainActivity.this.startActivity(intent);
}
}
}
第二个界面(ResultActivity):
public class ResultActivity extends Activity{
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
resultView = (TextView)findViewById(R.id.result);
//得到Intent对象当中的值
Intent intent = getIntent();
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
int factorOneInt = Integer.parseInt(factorOneStr);
int factorTwoInt = Integer.parseInt(factorTwoStr);
//计算两个值的积
int result = factorOneInt * factorTwoInt;
resultView.setText(result + "");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("ResultActivity--->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("ResultActivity--->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("ResultActivity--->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("ResultActivity--->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("ResultActivity--->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("ResultActivity--->onStop");
super.onStop();
}
}
我们来看看运行效果:
首先打开第一个Activity,输入2和3:
此时IDE打印出:
创建一个Activity调用onStart(可见)和onResume(可编辑)。
点击计算,马上跳转到新的界面:
此时IDE打印出:
第一个Activity调用onPause失去焦点,然后第二个Activity调用onStart和onResume,最后第一个Activity调用onStop变得不可见。
此时点击后退按钮回到MainActivity:
ResultActivity被销毁,MainActivity重新获得焦点,与前一篇博文的描述一致。
今天就谈到这里,下篇博文继续聊Activity之间的通信以及Activity一些剩余的东西~~