Data transmission and receipt during Android_06_Activity redirection

Source: Internet
Author: User

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 );}}

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.