Data or data packet Bundle is transmitted between activities, objects are transmitted, and objects are serialized. Objects implement the Parcelable interface and bundleparcelable
1 package com. gaojinhua. android. activitymsg; 2 3 import android. content. intent; 4 import android. OS. bundle; 5 import android. support. v7.app. appCompatActivity; 6 import android. view. view; 7 import android. widget. editText; 8 import android. widget. textView; 9 import android. widget. toast; 10 11 import java. util. regex. pattern; 12 13 public class MainActivity extends AppCompatActivity {14 public static final String DATA = "data"; 15 private EditText etName; 16 private EditText etAge; 17 private TextView textView; 18 private User user; 19 20 @ Override21 protected void onCreate (Bundle savedInstanceState) {22 super. onCreate (savedInstanceState); 23 setContentView (R. layout. activity_main); 24 25 etName = (EditText) findViewById (R. id. etName); 26 etAge = (EditText) findViewById (R. id. etAge); 27 textView = (TextView) findViewById (R. id. textView); 28 29 findViewById (R. id. btnStartSecond ). setOnClickListener (new View. onClickListener () {30 @ Override31 public void onClick (View v) {32 33 if (Pattern. compile ("[a-zA-Z] + "). matcher (etName. getText (). toString ()). matches () & 34 Pattern. compile ("[0-9] + "). matcher (etAge. getText (). toString ()). matches () {35 user = new User (etName. getText (). toString (), Integer. parseInt (etAge. getText (). toString (); 36 Bundle B = new Bundle (); 37 B. putParcelable (DATA, user); 38 Intent I = new Intent (MainActivity. this, SecondActivity. class); 39 I. putExtras (B); 40 startActivityForResult (I, 0); 41} else {42 Toast. makeText (getApplicationContext (), "Enter complete or correct data", Toast. LENGTH_SHORT ). show (); 43} 44 45} 46}); 47} 48 49 @ Override50 protected void onActivityResult (int requestCode, int resultCode, Intent data) {51 super. onActivityResult (requestCode, resultCode, data); 52 textView. setText ("the data returned by another Activity is:" + data. getExtras (). getString (DATA); 53} 54 55 56}MainActivity 1 package com. gaojinhua. android. activitymsg; 2 3 import android. content. intent; 4 import android. OS. bundle; 5 import android. support. v7.app. appCompatActivity; 6 import android. view. view; 7 import android. widget. textView; 8 9 public class SecondActivity extends AppCompatActivity {10 private TextView textViewAty; 11 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_second); 16 17 Intent I = getIntent (); 18 Bundle B = I. getExtras (); 19 User user = B. getParcelable (MainActivity. DATA); 20 21 textViewAty = (TextView) findViewById (R. id. textViewAty); 22 textViewAty. setText (String. format ("User info (name = % s, age = % d)", user. getName (), user. getAge (); 23 24 findViewById (R. id. btnBack ). setOnClickListener (new View. onClickListener () {25 @ Override26 public void onClick (View v) {27 Intent I = new Intent (); 28 Bundle B = new Bundle (); 29 B. putString (MainActivity. DATA, textViewAty. getText (). toString (); 30 I. putExtras (B); 31 setResult (1, I); 32 finish (); 33} 34}); 35} 36 37}SecondActivity
1 package com. gaojinhua. android. activitymsg; 2 3 import android. OS. parcel; 4 import android. OS. parcelable; 5 6/** 7 * Created by Gaojinhua on 2015/8/14. 8 */9 public class User implements Parcelable {10 public static final Creator <User> CREATOR = new Creator <User> () {11 @ Override12 public User createFromParcel (Parcel source) {13 return new User (source. readString (), source. readInt (); 14} 15 16 @ Override17 public User [] newArray (int size) {18 return new User [size]; 19} 20}; 21 private String name; 22 private int age; 23 24 25 public User (String name, int age) {26 this. name = name; 27 this. age = age; 28} 29 30 public String getName () {31 return name; 32} 33 34 public void setName (String name) {35 this. name = name; 36} 37 38 public int getAge () {39 return age; 40} 41 42 public void setAge (int age) {43 this. age = age; 44} 45 46 @ Override47 public int describeContents () {48 return 0; 49} 50 51 @ Override52 public void writeToParcel (Parcel dest, int flags) {53 dest. writeString (getName (); 54 dest. writeInt (getAge (); 55} 56 57 58}User
Data or data packet Bundle is transmitted between activities, objects are transmitted, and objects are serialized to implement the Parcelable interface.
Added Input Validation
Avoid accepting data with mismatched NULL data and types
Https://github.com/gaojinhua/ActivityMsg/tree/master/app/src/main/java/com/gaojinhua/android/activitymsg
Git: https://github.com/gaojinhua/ActivityMsg.git