1、Activity,可以认为应用程序的页面
四大组件使用时,必须在清单文件中进行注册
2、启动Activity
Intent:各组件之间的通信使用“意图”
// 1.本页面
// 2.跳转的目的页面
Intent intent = new Intent(MainActivity.this, NextActivity.class);
Intent intent = new Intent();
intent.setClass(MainActivity.this, NextActivity.class);
// 启动方式
startActivity(intent);
startActivityForResult();
3、页面之间传递数据
a:A页面可以使用Intent对象的putExtra方法设置名值对
B页面首先获得Intent(使用getIntent方法),之后可以使用相应的getStringExtra,getIntExtra方法获得值
b:使用Bundle(一捆)
方式一:
A页面:创建Bundle,将数据打包成捆,之后发送
Bundle bundle = new Bundle();
bundle.putString("sex", "male");
intent.putExtra("bundle", bundle);
B页面: 获得bundle,之后使用相应的get***方法获得值
Bundle bundle = intent.getBundleExtra("bundle");
String sex = bundle.getString("sex");
方式二:
A页面:intent.putExtras(bundle);
B页面:Bundle bundle = intent.getExtras();
使用方式二,可以获得之前页面使用putExtra方法存储的数据
传递对象:
A页面:bundle.putSerializable("person", person);
B页面:Person p = (Person) bundle.getSerializable("person");
4、启动方式,startActivityForResult();
跳转到其他Acitvity并返回结果
A页面:
首先跳转到B页面,代码如下:注意启动方式
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivityForResult(intent, 1);
接收B页面返回的数据,首先判断请求码和响应码是否一致
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == 2) {
Bundle bundle = data.getExtras();
mTvShowInfo.setText(bundle.getString("name"));
}
}
B页面:首先获得Intent,不要创建,通过getIntent方法获得
之后使用Bundle封装数据
setResult(),指定响应码,并返回数据
finish(),结束Acitvity生命周期,销毁B页面
Intent intent = getIntent();
// 值使用Bundle封装
Bundle bundle = new Bundle();
bundle.putString("name", "wangwu");
intent.putExtras(bundle);
// 设置响应吗,并返回数据
setResult(2, intent);
// 结束Activity的生命周期
finish();
5、生命周期方法:
onCreate(Bundle):创建并初始化时调用
onStart():显示页面(Activity)
onResume():获得用户焦点,可以与用户进行交互
onPause():失去用户焦点,暂停与用户的交互
onStop():关闭页面,处于后台执行
onDestroy():销毁Activity
onRestart():页面启动并回退,重新显示给用户,可以与用户进行交互,级联执行onStart-onResume
6、生命周期执行过程:
a:创建时,onCreate-onStart-onResume,启动应用程序
b:按HOME键时,onPause-onStop,失去焦点,关闭页面
c:再次运行应用程序,onRestart-onStart-onResume,重新获得焦点,显示页面
d:按回退键,onPause-onStop-onDestroy,退出应用程序
7、
the entire lifetime:完整生命周期
onCreate-onDestroy
the visible lifetime:可见生命周期
onStart-onStop
the foreground lifetime:焦点(前沿)生命周期
onResume-onPause
8、现场保护
当Activity非正常退出,或者横竖屏切换,需要保存一些数据
onSaveInstanceState:保存数据
onPause-onSaveInstanceState-onStop-onDestory
onRestoreInstanceState:恢复数据
onCreate-onStart-onRestoreInstanceState-onResume
现场保护案例:
public class MainActivity extends Activity {
private RelativeLayout layout;
private TextView mTextView;
String s = null;
int bgColor = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.layout);
mTextView = (TextView) findViewById(R.id.mTextView);
}
public void change(View v) {
switch (v.getId()) {
case R.id.changeText:
s = "android1522";
mTextView.setText(s);
break;
case R.id.changeBG:
int red = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
bgColor = Color.rgb(red, green, blue);
layout.setBackgroundColor(bgColor);
break;
}
}
// 保存数据
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (s != null) {
outState.putString("s", s);
}
if (bgColor != -1) {
outState.putInt("bgColor", bgColor);
}
}
// 恢复数据
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
s = savedInstanceState.getString("s");
bgColor = savedInstanceState.getInt("bgColor", -1);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (s != null) {
mTextView.setText(s);
}
if (bgColor != -1) {
layout.setBackgroundColor(bgColor);
}
}
}