Activity 四大状态:
活动状态:位于Activity栈顶 用户可见,获得焦点
暂停状态:失去焦点的Activity 用户不可见,在系统内存低的情况下不能被Killed
停止状态: 该Activity被其他Activity覆盖,用户不可见,在系统内存低的情况下会被Killed 仍然保存所有的状态和信息
销毁状态:该Activity结束 或Activity所在的Dalvik进程结束
Activity的回调方法:
onCreate() 在创建Activity的时候被回调
onStart() 在启动Activity的时候被回调
onRestart() 重新启动Activity的时候被回调
onPause() 暂停Activity的时候被回调
onResume() 当Activity由暂定状态恢复成活动状态被回调
onStop() 停止Activity时被回调
onDestroy() 销毁Activity时被回调
//启动 Activity
Intent mIntent=new Intent(MainActivity.this, Detaile.class); startActivity(mIntent);//关闭 Activity
finish();
public class AboutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this);//创建一个线性布局 ll.setPadding(20, 20, 20, 20); //设置线性布局的内边距 TextView tx = new TextView(this);//创建文本 tx.setTextSize(24);//设置文本大小 tx.setText("adasdjkaslkdjaskdljaskdjasdlkjasldkjaslkdjasdkjasdlk");//设置内容 ll.addView(tx); setContentView(ll); } }
<activity android:name=".AboutActivity" android:label="@string/about" android:theme="@android:style/Theme.Dialog" ><!-- 已Dialog的方式显示 --> </activity>
Bundle 是一个字符串到各种Parcelable类型的映射,用于保存要携带的数据包
//传送数据的Activity
String UserName=((EditText)findViewById(R.id.id_UserName)).getText().toString();//获取用户名 String PassWord=((EditText)findViewById(R.id.id_UserPass)).getText().toString();//获取用户密码 <span style="color:#FF6666;">Intent mIntent = new Intent(Register.this, Detaile.class); Bundle mBundle = new Bundle();//实例化一个Bundle对象</span> mBundle.putCharSequence("User", UserName);//保存用户名 mBundle.putCharSequence("pas", PassWord);//保存密码 <span style="color:#FF6666;">mIntent.putExtras(mBundle);//将创建好的Bundle添加到Intent中</span> startActivity(mIntent);//启动
private void GetIntent() { Intent mIntent=getIntent();//获取Intent对象 Bundle mBundle=mIntent.getExtras();// 获取传递的数据报 <span style="color:#FF0000;">String UserName=mBundle.getString("User").toString();</span> Toast.makeText(Detaile.this, UserName, Toast.LENGTH_SHORT).show(); }
1声明一个常量的CODE
2 使用 Intent mIntent = new Intent(Test001.this, Hand.class);
startActivityForResult(mIntent,Code2); 启动Activity
3.重写onActivityResult方法
4判断if (requestCode==CODE&&resultCode==CODE)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hand); mArrayList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < Img.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("Img", Img[i]); mArrayList.add(map); } SimpleAdapter mSimpleAdapter = new SimpleAdapter(Hand.this, mArrayList, R.layout.item, new String[] { "Img" }, new int[] { R.id.id_img }); ((GridView)findViewById(R.id.GridView1)).setAdapter(mSimpleAdapter); ((GridView)findViewById(R.id.GridView1)).setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent mIntent =getIntent(); Bundle mBundle=new Bundle(); mBundle.putInt("Image", Img[position]); mIntent.putExtras(mBundle); setResult(1, mIntent); finish(); } }); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO 自动生成的方法存根 super.onActivityResult(requestCode, resultCode, data); if (requestCode==CODE&&resultCode==CODE) { ((EditText)findViewById(R.id.editText1)).setText(""); ((EditText)findViewById(R.id.editText2)).setText(""); RadioGroup sexRadioGroup=(RadioGroup)findViewById(R.id.sex); String Sex=""; for (int i = 0; i < sexRadioGroup.getChildCount(); i++) { RadioButton mRadioButton=(RadioButton)sexRadioGroup.getChildAt(i); mRadioButton.setChecked(false); } } if(requestCode==Code2&&resultCode==Code2) { //获取选中的图像赋值 //取 data就是Intent Bundle mBundle=data.getExtras(); int InageID=mBundle.getInt("Image"); ((ImageView)findViewById(R.id.id_ImgView)).setImageResource(InageID); }