Intent 意图 用于Activity之间的数据传递

 http://blog.sina.com.cn/s/blog_83940dfb0100veas.html

用于Activity之间的数据传递

    在起始Activity中,发送数据

      protected void onCreate(Bundle saveInstanceState){

          super.onCreate(saveInstanceState);

          setContentView(R.layout.thisactivity);

          Intent intent = new Intent();

          //设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity

          intent.setClass(ThisActivity.this,TargetActivity.class);

          //绑定数据

          intent.putExtra("username",username);//也可以绑定数组

          intent.putExtra("userpass",userpass);

          //打开目标Activity

          startActivity(intent);

      }

    在目标Activity中,接收数据:

      protected void onCreate(Bundle saveInstanceState){

          super.onCreate(saveInstanceState);

          setContentView(R.layout.targetactivity);     

          //获得意图

          Intent intent = getIntent();

          //读取数据

          String name = intent.getStringExtra("username");

          String pass = intent.getStringExtra("userpass);

      }

也可以适用Bundle(捆)

    在起始Activity中,发送数据:

      protected void onCreate(Bundle saveInstanceState){

          super.onCreate(saveInstanceState);

          setContentView(R.layout.thisactivity);     

          Intent intent = new Intent();

          //设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity

          intent.setClass(ThisActivity.this,TargetActivity.class);

          //一次绑定多个数据

          Bundle bundle = new Bundle();

          bundle.putString("username",username);

          bundle.putString("userpass",userpass);

          intent.putExtras(bundle);

          //打开目标Activity

          startActivity(intent);

      }

    在目标Activity中,接收数据:

      protected void onCreate(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");

      }

当需要从目标Activity回传数据到原Activity时,可以使用上述方法定义一个新的Intent来传递数据,也可以使用startActivityForResult(Intent intent, int requestCode);方法。

    在起始Activity中,发送数据:

      protected void onCreate(Bundle saveInstanceState){

          super.onCreate(saveInstanceState);

          setContentView(R.layout.thisactivity);     

          Intent intent = new Intent();

          //设置起始Activity和目标Activity,表示数据从这个Activity传到下个Activity

          intent.setClass(ThisActivity.this,TargetActivity.class);

          //绑定数据

          intent.putExtra("username",username);//也可以绑定数组

          intent.putExtra("userpass",userpass);

          //打开目标Activity

          startActivityForResult(intent,1);

      }

      //需要重写onActivityResult方法

      protected void onActivityResult(int requestCode, int resultCode, Intent intent){

         super.onActivityResult(requestCode,resultCode,intent);

         //判断结果码是否与回传的结果码相同

         if(resultCode == 1){

            //获取回传数据

            String name = intent.getStringExtra("name");

            String pass = intent.getStringExtra("pass);

            //对数据进行操作

            ......

         }

    在目标Activity中,接收数据:

      protected void 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();

      }

你可能感兴趣的:(Activity)