android 4组件之 activity


1.startActivity启动其他activity,并传递参数

										Intent intent=new Intent();
										intent.setClass(LoginActivity.this,MainTabActivity.class);
										Bundle bundle=new Bundle();
										bundle.putByteArray("id", recV.szValue);
										intent.putExtras(bundle);
			
										startActivity(intent);

在目标activity的onCreate方法中获取参数
			Bundle bundle = getIntent().getExtras(); 
			byte[] value=bundle.getByteArray("id");
			byte[] b=new byte[4];
			while(true)
			{
				b[0]=value[i];
				b[1]=value[i+1];
				b[2]=value[i+2];
				b[3]=value[i+3];
				i+=4;

				int id=SmtApplication.byte2int(b);
				if(id==0)
					break;
				
				SmtApplication.m_vId.add(id);  

			}
				




2.使用startActivityForResult
在 AActivity中启动BActivity
			Intent intent=new Intent();
			intent.setClass(LoginActivity.this,AActivity.class);
			startActivityForResult(intent, 10);


在BActivity中的返回代码
							try {
								Intent intent=new Intent();
								Bundle bundle=new Bundle();
								bundle.putInt("id", 123456);
								intent.putExtras(bundle);
								setResult(10,intent);
								finish();
								
							} catch (Exception e) {
								// TODO: handle exception
							Log.e("tag", e.toString());
								
							}


在AAcitivity中出处理B的返回值,重载onActivityResult即可
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		
		switch(requestCode)
		{
			case 10:
			{
				try {
					
					Bundle bundle = data.getExtras();
					int id=bundle.getInt("id");
					if(id!=0)
						m_editid.setText(String.valueOf(id));
					
				} catch (Exception e) {
					// TODO: handle exception
					Log.e("tag"," onActivityResult "+e.toString());
				}

				
			}
			
		}
	
	
	}


你可能感兴趣的:(android 4组件之 activity)