Transfer of data between-activity and intent data transfer

Source: Internet
Author: User

Intent intent can be used for data transfer between activity, and can generally be divided into the following two cases, from the current activity to the target activity has no return value: 1. No return value after delivery:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950    在起始Activity中,发送数据     protectedvoidonCreate(Bundle saveInstanceState){         super.onCreate(saveInstanceState);         setContentView(R.layout.thisactivity);          Intent intent = newIntent();         //设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity         intent.setClass(ThisActivity.this,TargetActivity.class);         //绑定数据         intent.putExtra("username",username);//也可以绑定数组         intent.putExtra("userpass",userpass);         //打开目标Activity         startActivity(intent);      }    在目标Activity中,接收数据:     protectedvoidonCreate(Bundle saveInstanceState){         super.onCreate(saveInstanceState);         setContentView(R.layout.targetactivity);              //获得意图         Intent intent = getIntent();        //读取数据        String name = intent.getStringExtra("username");        String pass = intent.getStringExtra("userpass);     }也可以适用Bundle(捆)    在起始Activity中,发送数据:     protectedvoidonCreate(Bundle saveInstanceState){         super.onCreate(saveInstanceState);         setContentView(R.layout.thisactivity);              Intent intent = newIntent();         //设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity         intent.setClass(ThisActivity.this,TargetActivity.class);         //一次绑定多个数据        Bundle bundle = newBundle();        bundle.putString("username",username);         bundle.putString("userpass",userpass);         intent.putExtras(bundle);        //打开目标Activity        startActivity(intent);     }    在目标Activity中,接收数据:     protectedvoidonCreate(Bundle saveInstanceState){         super.onCreate(saveInstanceState);         setContentView(R.layout.targetactivity);              //获得意图         Intent intent = getIntent();         //读取数据          Bundle bundle = intent.getExtras();         String name = bundle.getString("username");         String pass = bundle.getString("userpass");     }

2. Return value after delivery: When you need to transfer data from the target activity to the original activity, you can use the above method to define a new Intent to pass the data, or you can use Startactivityforresult (Intent Intent, int requestcode); method.

1234567891011121314151617181920212223242526272829303132333435363738394041424344    在起始Activity中,发送数据:     protectedvoid onCreate(Bundle saveInstanceState){         super.onCreate(saveInstanceState);         setContentView(R.layout.thisactivity);              Intent intent = newIntent();        //设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity        intent.setClass(ThisActivity.this,TargetActivity.class);        //绑定数据        intent.putExtra("username",username);//也可以绑定数组        intent.putExtra("userpass",userpass);        //打开目标Activity         startActivityForResult(intent,1);     }     //需要重写onActivityResult方法     protectedvoidonActivityResult(intrequestCode, intresultCode, Intent intent){        super.onActivityResult(requestCode,resultCode,intent);         //判断结果码是否与回传的结果码相同        if(resultCode == 1){           //获取回传数据           String name = intent.getStringExtra("name");            String pass = intent.getStringExtra("pass);            //对数据进行操作           ......         }    在目标Activity中,接收数据:     protectedvoid onCreate(Bundle saveInstanceState){         super.onCreate(saveInstanceState);         setContentView(R.layout.targetactivity);              //获得意图         Intent intent = getIntent();         //读取数据         String name = intent.getStringExtra("username");         String pass = intent.getStringExtra("userpass);         //从EditText中获取新的数据给name和pass         name = editText1.getText().toString();         pass = editText2.getText().toString()         //数据发生改变,需要把改变后的值传递回原来的Activity         intent.putExtra("name",name);        intent.putExtra("pass",pass);        //setResult(int resultCode,Intent intent)方法        setResult(1,intent);        //销毁此Activity,摧毁此Activity后将自动回到上一个Activity        finish();}

Transfer of data between-activity and intent data transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.