两个activity之间的Intent 传值

阅读更多

看了很多,总结以下首先activity1和activity2之间

 

activity1的时候

传值{

   Intent intent = new Intent();
   intent.setClass(SoundListenActivity.this, appset.class);
   Bundle bundle =new Bundle();
   bundle.putString("phonenum", phonenum);
   bundle.putLong("threhode", threhode);
   intent.putExtras(bundle);
   startActivityForResult(intent, 1);

 

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
   if (resultCode == 1) {
   Bundle bundle = data.getExtras();
   
   phonenum = bundle.getString("phonenum");
   threhode = bundle.getLong("threhode");

}

 

activity2里面

 

protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.appset);
  
   Intent intent =new Intent();
  Bundle bundle =getIntent().getExtras();
  String phonenum=bundle.getString("phonenum");
  Long threhode=bundle.getLong("threhode");
  System.out.println("appset-->>"+phonenum+">>"+threhode);
    
 }

 

返回传值的程序是

Intent intent = new Intent();
    intent.setClass(appset.this, SoundListenActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("phonenum", phone);
    bundle.putLong("threhode", threhode);
    intent.putExtras(bundle);
    setResult(1,intent);
    finish();

 

加粗的那几个地方,需要注意!谢谢

你可能感兴趣的:(android,Intent,java)