There are so many ways to pass data in Android, and this is the most common method of data delivery in 4:
1. Passing Data through intent
2. Passing data through static variables (static)
3. Passing data through the Clipboard (Clipboard)
4. Passing data through global objects
In the Transmitdataactivity.java
Package mobile.android.transmit.data; public class Transmitdataactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.
OnCreate (savedinstancestate);
Setcontentview (R.layout.main);
Button button1= (button) Findviewbyid (R.id.button1);
Button button2= (button) Findviewbyid (R.id.button2);
Button button3= (button) Findviewbyid (R.id.button3);
Button button4= (button) Findviewbyid (R.ID.BUTTON4);
Button1.setonclicklistener (New Buttonlistenner ());
Button2.setonclicklistener (New Buttonlistenner ());
Button3.setonclicklistener (New Buttonlistenner ());
Button4.setonclicklistener (New Buttonlistenner ()); Class Buttonlistenner implements onclicklistener{@SuppressLint ("Newapi") @Override public void OnClick (view view) {Int
ENT intent=null; Switch (View.getid ()) {Case R.id.button1:intent=new Intent (transmitdataactivity.this,myactivity1.class);
Holds the string type value Intent.putextra ("Intent_string", "string passed by intent");
Saves the integer type value Intent.putextra ("Intent_integer", 300); DATA data=new data ();
data.id=1000;
Data.name= "Android";
Save serializable object Intent.putextra ("Intent_object", data);
Displays the activity startactivity (intent) used to receive data;
Break
Case R.id.button2:intent=new Intent ();
Intent.setclass (Transmitdataactivity.this, Myactivity2.class);
The following code assigns three static variables to the MyActivity2 myactivity2.id=3000;
Myactivity2.name= "Porsche";
Myactivity2.data=new data ();
myactivity2.data.id=5555;
Myactivity2.name= "Android";
StartActivity (Intent);
Break
Case R.id.button3:intent=new Intent (transmitdataactivity.this,myactivity3.class); Gets the Clipboard object (Clipboardmanager) Clipboardmanager clipboard= (Clipboardmanager) Getsystemservice (context.clipboard_
SERVICE);
Create data Object Data clipboarddata=new data ();
Sets the value of the field in the data object clipboarddata.id=6666;
Clipboarddata.name= "Data transmitted through clipboard";
Creates a byte array output stream object for converting the data object to a byte stream bytearrayoutputstream baos=new bytearrayoutputstream ();
String base64str= "" used to save the Base64 format of the data object generation; try {objectoutputstream oos=new objectoutputstream (BAOs);//write the data object to the object output stream Oos.writeobject (clIpboarddata);
The byte stream is BASE64 encoded base64str=base64.encodetostring (Baos.tobytearray (), base64.default);
Oos.close (); The catch (IOException e) {e.printstacktrace ();}//Get the Clipboard data Object (Clipdata) that stores the text data clipdata clipdata=clipdata.newplaintext ("
Data ", BASE64STR);
Sets the main clipboard clipboard.setprimaryclip (Clipdata);
Display Myactivity3 window startactivity (intent);
Break
Case R.ID.BUTTON4://Get MyApp object MyApp myapp= (MyApp) getapplicationcontext ();
Myapp.country= "The United States";
myapp.data.id=1234;
Myapp.data.name= "flying saucer";
Intent=new Intent (Transmitdataactivity.this,myactivity4.class);
StartActivity (Intent);
Break
Default:break; }
}
}
}
Data class: This class is serializable, which is the class that implements the Java.io.Serializable interface.
Package mobile.android.transmit.data;
Import java.io.Serializable;
public class Data implements serializable{public
int id;
public String name;
In the Myactivity1.java: Package mobile.android.transmit.data;
public class MyActivity1 extends activity{
@Override
protected void onCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
super.oncreate (Savedinstancestate);
Setcontentview (r.layout.myactivity);
TextView textview= (TextView) Findviewbyid (R.id.textview);
Gets the string value
string Intentstring=getintent (). Getstringextra ("intent_string");
Gets the integer type value
int intentinteger=getintent (). Getextras (). GetInt ("Intent_integer");
Gets the data type value of
data data= (data) getintent (). Getextras (). Get ("Intent_object");
StringBuffer sb=new StringBuffer ();
Sb.append ("intent_string:");
Sb.append (intentstring);
Sb.append ("\ n");
Sb.append ("Intent_integer:");
Sb.append (Intentinteger);
Sb.append ("\ n");
Sb.append ("data.id:");
Sb.append (data.id);
Sb.append ("\ n");
Sb.append ("Data.name:");
Sb.append (data.name);
Sb.append ("\ n");
Output the value passed over the screen
Textview.settext (sb.tostring ());
}
In the Myactivity2.java:
Package mobile.android.transmit.data;
public class MyActivity2 extends activity{public
static String name;
public static int id;
public static data data;
@Override
protected void onCreate (Bundle savedinstancestate) {
//TODO auto-generated method stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.myactivity);
TextView textview= (TextView) Findviewbyid (R.id.textview);
StringBuffer sb=new StringBuffer ();
Sb.append ("Name:");
Sb.append (name);
Sb.append ("\ n");
Sb.append ("ID:");
Sb.append (ID);
Sb.append ("\ n");
Sb.append ("data.id:");
Sb.append (data.id);
Sb.append ("\ n");
Sb.append ("Data.name:");
Sb.append (data.name);
Sb.append ("\ n");
Textview.settext (Sb.tostring ());
}
In the Myactivity3:
package mobile.android.transmit.data; public class MyActivity3 extends @ Suppresslint ("Newapi") @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub s
Uper.oncreate (savedinstancestate);
Setcontentview (r.layout.myactivity);
TextView textview= (TextView) Findviewbyid (R.id.textview);
Gets the Clipboard object Clipboardmanager clipboardmanager= (Clipboardmanager) Getsystemservice (Context.clipboard_service);
Gets the BASE64 encoded string Base64str=clipboardmanager.getprimaryclip (). Getitemat (0). GetText (). toString ();
Encodes a Base64 encoded string into a byte array byte[] Buffer=base64.decode (BASE64STR, Base64.default);
Bytearrayinputstream bais=new bytearrayinputstream (buffer); try {objectinputstream ois=new objectinputstream (Bais);//convert byte stream to data Object data data= (data) ois.readobject (); Display the BASE64 encoded original and data object field values in the TextView control Textview.settext (base64str+ "\n\ndata.id:" +data.id+ "\ndata.name:" +data.name)
;
catch (Exception e) {e.printstacktrace ();} }
}
In the Myactivity4.java:
Package mobile.android.transmit.data;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class MyActivity4 extends activity{
@Override
protected void onCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
super.oncreate (Savedinstancestate);
Setcontentview (r.layout.myactivity);
TextView textview= (TextView) Findviewbyid (R.id.textview);
MYAPP myapp= (MYAPP) Getapplicationcontext ();
Textview.settext ("Myapp.country:" +myapp.country+ "\nmyapp.data.id:" +myapp.data.id+ "\nmyapp.data.name:" + myApp.data.name);
}
About four kinds of common methods of transferring data between different activity are introduced so many, hope to help everyone!