Activity状态保存
1.onSaveInstanceState()/onCreate()/onRestoreInstanceState()
系统在回收当期Activity之前会调用onSaveInstanceState(),Back&Home两个键不会调用此方法.例如电子书程序,阅读到某一页内存不足被系统回收.可以通过此方法记录第几页.在程序重新启动时,在onCreate()里判断重新读取退出时第几页状态.它的参数Bundle是key-value形式
private final String SAVE_INSTANCE_TAG = "MainTestActivity.Saveinstancetag";
EditText editText = null;
@Override
protected void onSaveInstanceState(Bundle outState) {
if(outState!=null){
outState.putString(this.SAVE_INSTANCE_TAG, "hello!!!!");
}
System.out.println("onSaveInstanceState");
super.onSaveInstanceState(outState);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("onCreate");
editText = (EditText)findViewById(R.id.myEditText);
if(editText!=null&&savedInstanceState!=null){
editText.setText(savedInstanceState.getString(SAVE_INSTANCE_TAG));
}
if(savedInstanceState!=null){
System.out.println("savedInstanceState"+savedInstanceState);
}
}
onRestoreInstanceState()不一定是与onSaveInstanceState()成对使用的.调用此方法时.Activity定是被系统Destroy掉的.其Bundle参数值也可以传到onCreate()
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
String tag= savedInstanceState.getString(this.SAVE_INSTANCE_TAG);
super.onRestoreInstanceState(savedInstanceState);
}
2.借助Activity Lifecycle+Preference
使用这种方式状态保存可以是.横竖屏幕切换,按Home or Back键之后再回来.
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// 写入 SharedPreferences
SharedPreferences preferences = getSharedPreferences("name",
MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putBoolean("boolean_key", true);
editor.putString("string_key", "string_value");
editor.commit();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// 读取 SharedPreferences
SharedPreferences preferences = getSharedPreferences("name",
MODE_PRIVATE);
preferences.getBoolean("boolean_key", false);
preferences.getString("string_key", "default_value");
}
Activity之间通信
1.无参数Activity跳转
Intent intent = new Intent(Activity1.this, Activity2.class); startActivity(intent);
2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)
Intent intent = new Intent();
intent.setClass(Ex03_10Activity.this, Ex03_10_1.class);
Bundle bundle = new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
intent.putExtras(bundle);
startActivity(intent); //startActivityForResult(intent, 0);
//数据的接收
Bundle bundle = this.getIntent().getExtras();
String sex = bundle.getString("sex");
double height = bundle.getDouble("height");
3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)
Intent intent=getIntent();
Bundle bundle2=new Bundle();
bundle2.putString("sex", "Man!");
intent.putExtras(bundle2);
setResult(RESULT_OK, intent);
4.回调上一个Activity的结果处理函数(onActivityResult)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case RESULT_OK:
Bundle bundle = data.getExtras();
String sex = bundle.getString("sex");
double height = bundle.getDouble("height");
et.setText(""+height);
if(sex.equals("M")){
rb1.setChecked(true);
}else{
rb2.setChecked(true);
}
break;
default:
break;
}
}
打电话
1. //叫出拨号程序
2. Uri uri = Uri.parse("tel:0800000123");
3. Intent it = new Intent(Intent.ACTION_DIAL, uri);
4. startActivity(it);
1. //直接打电话出去
2. Uri uri = Uri.parse("tel:0800000123");
3. Intent it = new Intent(Intent.ACTION_CALL, uri);
4. startActivity(it);
5. //用這個,要在 AndroidManifest.xml 中,加上
6. //<uses-permission id="android.permission.CALL_PHONE" />
传送SMS/MMS
1. //调用短信程序
2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
3. it.putExtra("sms_body", "The SMS text");
4. it.setType("vnd.android-dir/mms-sms");
5. startActivity(it);
1. //传送消息
2. Uri uri = Uri.parse("smsto://0800000123");
3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
4. it.putExtra("sms_body", "The SMS text");
5. startActivity(it);
1. //传送 MMS
2. Uri uri = Uri.parse("content://media/external/images/media/23");
3. Intent it = new Intent(Intent.ACTION_SEND);
4. it.putExtra("sms_body", "some text");
5. it.putExtra(Intent.EXTRA_STREAM, uri);
6. it.setType("image/png");
7. startActivity(it);
以上应用实例更多请参考原作者http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html