Data transmission and receipt during Android_06_Activity redirection
MainActivity. java
package com.itheima.senddata;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){
// This is also a startup method, which writes two steps as one step; Intent intent = new Intent (this, SecondActivity. class); // Method 1: encapsulate the data directly into the intent object // encapsulate the data into the intent object // intent. putExtra (malename, Li Zhi); // intent. putExtra (femalename, Sister Furong); // Method 2: encapsulate data into the bundle object first, then, // encapsulate the bundle object into the intent object. // encapsulate the data into the bundle object. Bundle bundle = new Bundle (); bundle. putString (malename, Li Zhi); bundle. putString (femalename, Sister Furong); // encapsulate the bundle object into the intent object. putExtras (bundle); startActivity (intent );}}
SecondActivity. java
Package com. itheima. senddata; import java. util. random; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class SecondActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_second); Intent intent = getIntent (); // how the data is encapsulated, then, extract the encapsulated data from the intent object // String maleName = intent. getStringExtra (malename); // String feMaleName = intent. getStringExtra (femalename); Bundle bundle = intent. getExtras (); String maleName = bundle. getString (malename); String feMaleName = bundle. getString (femalename); Random rd = new Random (); int yinyuan = rd. nextInt (100); TextView TV = (TextView) findViewById (R. id. TV); TV. setText (the Marriage value of maleName + and + feMaleName + is + yinyuan );}}