Activity,是Android中一个重要的组件。就像任务管理进程,每个Activity负责一个任务,一般都是一个页面对应一个Activity,Activity负责响应当前页面的所有请求和处理。而Activity的生命周期,也和进程很类似,它有创建(OnCreate)、开始(onStart)、暂停(onPause)、唤醒(onResume)、停止(onStop)、重启(onRestart)、销毁(onDestroy)等状态。下面一幅图,清楚地描述了这些状态的转变:
Activity启动的流程包括:onCreate()->onStart()->onResume()
Activity关闭的流程包括:onPause()->onStop()->onDestroy()。
Activity重启的流程包括:onPause()->onResume()或者onStop()->onRestart()
下面再通过实例来讲述Activity的生命周期:
(1)启动Activity1
(2)从Activity1中启动Activity2(注意Activity1这里并没有调用finish()方法)
(3)从Activity2中返回Activity1(注意Activity2这里调用了finish()方法,会引发onDestroy()方法)
(4)退出Activity1,程序结束
可以看出Activity1经历的生命周期为:onCreate()->onStart()->onResume()->onPause()->onStop()->onRestart()->onResume()->onPause()->onStop()->onDestroy()
Activity2经历的生命周期为:onCreate()->onStart()->onResume()->onPause()->onStop()->onDestroy()
实例视图与具体代码如下:
1、布局文件
Activity1的布局文件main.xml:
</LinearLayout>
Activity2的布局文件 mylayout.xml:
2、代码
Activity1的程序代码
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ActivityIntent extends Activity {
private Button bt_toActivity;
private Button bt_outsideIntent;
private EditText et_string;
private TextView tv_result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e("Lifecycle_Activity1", "onCreate()");
et_string = (EditText) findViewById(R.id.et_string);
bt_toActivity = (Button) findViewById(R.id.bt_toActivity2);
bt_outsideIntent = (Button) findViewById(R.id.bt_url);
tv_result = (TextView) findViewById(R.id.tv_result);
bt_toActivity.setOnClickListener(new ClickEvent());
bt_outsideIntent.setOnClickListener(new ClickEvent());
}
//销毁:onPause()->onStop()->onDestroy()
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("Lifecycle_Activity1", "onDestroy()");
}
//暂停:onStart()->onPause()
@Override
protected void onPause() {
super.onPause();
Log.e("Lifecycle_Activity1", "onPause()");
//把数据保存到类似于Session之类的存储集合里面
SharedPreferences.Editor saveData = getPreferences(0).edit();
saveData.putString("value", et_string.getText().toString());
saveData.commit();
}
//重启:onPause()->onResume()
@Override
protected void onResume() {
super.onResume();
Log.e("Lifecycle_Activity1", "onResume()");
//从共享数据存储对象中获取所需的数据
SharedPreferences getData = getPreferences(0);
String value = getData.getString("value", null);
if(value != null) {
et_string.setText(value);
}
}
//开始:onCreate()->onStart()
@Override
protected void onStart() {
super.onStart();
Log.e("Lifecycle_Activity1", "onStart()");
}
//停止:onPause()->onStop()
@Override
protected void onStop() {
super.onStop();
Log.e("Lifecycle_Activity1", "onStop()");
}
//重启:onStop()->reonRestart()
@Override
protected void onRestart() {
super.onRestart();
Log.e("Lifecycle_Activity1", "onRestart()");
}
/**
* 处理上一个Activity的返回结果
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) {
case RESULT_OK: //执行成功,返回
Bundle bundle = data.getExtras(); //获取intent里面的bundle对象
int length = bundle.getInt("result");
tv_result.setText("" + length);
break;
default:
break;
}
}
//重写点击监听器类
class ClickEvent implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bt_toActivity2: //跳转到另一个Activity
Intent intent = new Intent();
intent = intent.setClass(ActivityIntent.this, AnotherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("string", et_string.getText().toString());
intent.putExtras(bundle);
startActivityForResult(intent,0); //采用Activity间信息交流启动方式,
//比如A启动B,B结束后,返回到A的同时会执行 onActivityResult()方法
//处理上一个Activity即B传递的结果集。
//ActivityIntent.this.finish(); //会触发onDestroy();
break;
case R.id.bt_url: //Intent还可以调用外部组件,但有些组件会有权限控制
Uri uri = Uri.parse("http://www.baidu.com");
Intent outerIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(outerIntent);
break;
default:
break;
}
}
}
}
Activity2的程序代码
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AnotherActivity extends Activity {
private String string;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Log.e("Lifecycle_Activity1", "onCreate()");
Intent intent = this.getIntent(); //获取intent对象
Bundle bundle = intent.getExtras(); //获取intent里面的bundle对象
string = bundle.getString("string"); //获取字符串
((TextView)findViewById(R.id.tv_showResult)).setText(
"字符串 " + string +" 长度为: " + string.length());
Button bt_back = (Button) findViewById(R.id.bt_back);
bt_back.setOnClickListener(new ClickEvent());
}
@Override
protected void onDestroy() { //销毁:onPause()->onStop()->onDestroy()
// TODO Auto-generated method stub
super.onDestroy();
Log.e("Lifecycle_Activity2", "onDestroy()");
}
@Override
protected void onPause() { //暂停:onStart()->onPause()
// TODO Auto-generated method stub
super.onPause();
Log.e("Lifecycle_Activity2", "onPause()");
}
@Override
protected void onResume() { //重启:onPause()->onResume()
// TODO Auto-generated method stub
super.onResume();
Log.e("Lifecycle_Activity2", "onResume()");
}
@Override
protected void onStart() { //开始:onCreate()->onStart()
// TODO Auto-generated method stub
super.onStart();
Log.e("Lifecycle_Activity2", "onStart()");
}
@Override
protected void onStop() { //停止:onPause()->onStop()
// TODO Auto-generated method stub
super.onStop();
Log.e("Lifecycle_Activity2", "onStop()");
}
@Override
protected void onRestart() { //重启
// TODO Auto-generated method stub
super.onRestart();
Log.e("Lifecycle_Activity2", "onRestart()");
}
class ClickEvent implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bt_back:
Intent intent = new Intent();
intent = intent.setClass(AnotherActivity.this, ActivityIntent.class);
Bundle bundle = new Bundle();
bundle.putInt("result", string.length());
intent.putExtras(bundle);
//返回刚刚创建的intent给上一个Activity,并且关闭当前Activity
AnotherActivity.this.setResult(RESULT_OK, intent);
AnotherActivity.this.finish(); //会触发onDestroy();
break;
default:
break;
}
}
}
}
最后记得在AndroidManifest.xml添加Activity2的信息,如下:
下一篇补充下Activity之间的跳转和传值,以及从如何获取上一个Activity返回的结果和数据状态保存等内容。简单的说,Intent、Bundle、SharedPreferences等应用。