1.四种状态
running/paused/stopped/killed
2.activity生命周期分析
(1) onCreate()—>onStart()—>onResume()—>onPause()—>Onstop()—>OnDestory
(2) onCreate()—>onStart()—>onResume()—>onPause()—>onResume()—>……
(3) onCreate()—>onStart()—>onResume()—>onPause()—>Onstop()—>onRestart()—>onStart()……
3.activity进程优先级
后进先出;任务栈不是唯一的,可能有多个。
通过Intent传递数据
Intent intent =new Intent(CurrentActivity.this,OtherActivity.class);
// 创建一个带“收件人地址”的 email
Bundle bundle =new Bundle();// 创建 email 内容
bundle.putBoolean("boolean_key", true);// 编写内容
bundle.putString("string_key", "string_value");
intent.putExtra("key", bundle);// 封装 email
startActivity(intent);// 启动新的 Activity
Intent intent =getIntent();// 收取 email
Bundle bundle =intent.getBundleExtra("key");// 打开 email
bundle.getBoolean("boolean_key");// 读取内容
bundle.getString("string_key");
//====================设置数据=====================================
Intent intent =new Intent(EX06.this,OtherActivity.class);
intent.putExtra("boolean_key", true);
intent.putExtra("string_key", "string_value");
startActivity(intent);
//====================获取数据=====================================
Intent intent=getIntent();
intent.getBooleanExtra("boolean_key",false);
intent.getStringExtra("string_key");
适用于不可序列化的且简单的对象
如果想使某些数据长时间驻留内存,以便程序随时调用,建议采用全局对象。Application全局类不需要定义静态变量只要定义普通成员变量即可,但全局类中必须有一个无参构造方法.
public class MainApplication extends Application {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
public class MainActivity extends Activity {
private MainApplication application;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
application = (MainApplication) getApplication();
application.setUsername("sunzn");
}
public void open(View view) {
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
}
}
public class OtherActivity extends Activity {
private TextView tv_data;
private MainApplication application;
private String username;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
application = (MainApplication) getApplication();
username = application.getUsername();
tv_data = (TextView) findViewById(R.id.tv_data);
tv_data.setText("从上一个 Activity 中获取到的数据为:" + username);
}
}
Activity中的相关方法:
例子:从Activity1跳转到Activity2,从Activity2返回数据到Aactivity1
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); //只有这里不同
Intent intent = new Intent();
intent = intent.setClass(AnotherActivity.this, ActivityIntent.class);
Bundle bundle = new Bundle();
bundle.putInt("result", "Activity2的处理结果");
intent.putExtras(bundle);
AnotherActivity.this.setResult(RESULT_OK, intent); //RESULT_OK是返回状态码
AnotherActivity.this.finish();
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) { //根据状态码(在startActivityForResult中设置的),处理返回结果
case RESULT_OK:
Bundle bundle =data.getExtras(); //获取intent里面的bundle对象
String result = bundle.getInt("result");
break;
default:
break;
}
onSaveInstanceState(Bundle outState),系统决定是否调用此方法。如果调用,此方法将在onStop之前发生,不保证是否会在onPause之前或之后发生。
public class MainActivity extends Activity {
private String temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 从savedInstanceState中恢复数据, 如果没有数据需要恢复savedInstanceState为null
if (savedInstanceState != null) {
temp = savedInstanceState.getString("temp");
System.out.println("onCreate: temp = " + temp);
}
}
public void onRestoreInstanceState(Bundle saveInstanceState) {
super.onRestoreInstanceState( saveInstanceState);
String temp = saveInstanceState.getString("temp");
System.out.println("onResume: temp = " + temp);
}
// 将数据保存到outState对象中, 该对象会在重建activity时传递给onCreate方法和onRestoreInstanceState方法
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("temp", temp);
}