欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

两个activity之间的Intent 传值 博客分类: 经验总结javaAndroid androidIntentjava 

程序员文章站 2024-02-07 13:58:10
...

看了很多,总结以下首先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();

 

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