|
一、直接打开,不传递参数 Intent intent = new Intent(this, Activity.class);startActivity(intent);
二、传递参数
public void OpenNew(View v) { //新建一个显式意图,第一个参数为当前Activity类对象,第二个参数为你要打开的Activity类 Intent intent =new Intent(MainActivity.this,MainActivity2.class); //用Bundle携带数据 Bundle bundle=new Bundle(); //传递name参数为tinyphp bundle.putString("name", "tinyphp"); intent.putExtras(bundle); startActivity(intent);
//1.要关闭的页面 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.otheractivity); Intent intent = this.getIntent(); intent.putExtra("tel", 12345); //设置requestCode和带有数据的intent对象 OtherActivity.this.setResult(3, intent); //马上关闭Activity this.finish(); } //2.上面的页面关闭时,此页面进行数据的接收 class ButtonListener implements android.view.View.OnClickListener{ @Override public void onClick(View arg0) { Intent intent = new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); //与普通的start方法不同,需要设置requestCode startActivityForResult(intent, 1); } } //如果要进行此操作,需要在数据接收页面中复写activity的onActivityResul()方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); int tel = 0; //根据返回码resultCode来判断下一步进行的业务代码 if(resultCode==3){ tel = data.getIntExtra("tel", 0); } Log.i(TAG, "tel--------->"+String.valueOf(tel)); } }
三、回传参数
|