Android:Activity之間傳遞引數並跳轉
阿新 • • 發佈:2019-01-04
當前頁面傳出引數:
新頁面接收引數:public void First(View v){ //新建一個顯式意圖,第一個引數為當前Activity類物件,第二個引數為你要開啟的Activity類 Intent intent =new Intent(FirstActivity.this,SecondActivity.class); //用Bundle攜帶資料 Bundle bundle=new Bundle(); //傳遞name引數 bundle.putString("name", "data"); intent.putExtras(bundle); startActivity(intent); }
引數傳遞支援多個引數,同樣使用,多次使用:protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.newtest); //新頁面接收資料 Bundle bundle = this.getIntent().getExtras(); //接收name值 String name = bundle.getString("name"); Log.i("獲取到的name值為",name); }
bundle.putString("name0", "data");
intent.putExtras(bundle);
bundle.putString("name1", "data"); intent.putExtras(bundle);注意設定不同的標籤,在接收時分別接收即可:
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("name0");
Bundle bundle = this.getIntent().getExtras(); String name = bundle.getString("name1");