Four common methods for transferring data between different activities, Activity Four

Source: Internet
Author: User

Four common methods for transferring data between different activities, Activity Four

There are many ways to transmit data in Android. This section describes the commonly used data transfer methods in Section 4:

1. Transmit data through Intent

2. Transmit data through static variables

3. pass data through the Clipboard (Clipboard)

4. pass data through global objects

In 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 ){
Intent intent = null;
Switch (view. getId ()){
Case R. id. button1:
Intent = new Intent (TransmitDataActivity. this, MyActivity1.class );
// Save the String type value
Intent. putExtra ("intent_string", "string passed through Intent ");
// Save the integer value
Intent. puttextra ("intent_integer", 300 );
Data data = new Data ();
Data. id = 1000;
Data. name = "Android ";
// Save the serializable object
Intent. putExtra ("intent_object", data );
// Display the Activity used to receive data
StartActivity (intent );
Break;
Case R. id. button2:
Intent = new Intent ();
Intent. setClass (TransmitDataActivity. this, MyActivity2.class );
// The following Code assigns values to three static variables in 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 );
// Obtain the clipboard object (ClipboardManager)
ClipboardManager clipboard = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE );
// Create a Data Object
Data clipboardData = new Data ();
// Set the field value in the Data Object
ClipboardData. id = 6666;
ClipboardData. name = "data transmitted through Clipboard ";
// Create a byte array output stream object to convert the Data object to a byte stream
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
// Saves the Data object to generate a string in Base64 format
String base64Str = "";
Try {
ObjectOutputStream oos = new ObjectOutputStream (baos );
// Write the Data object to the object output stream
Oos. writeObject (clipboardData );
// Base64 encode the byte stream
Base64Str = Base64.encodeToString (baos. toByteArray (), Base64.DEFAULT );
Oos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
// Obtain the Clipboard data object (ClipData) that stores text data)
ClipData clipData = ClipData. newPlainText ("data", base64Str );
// Set the main clipboard
Clipboard. setPrimaryClip (clipData );
// Display the Myactivity3 window
StartActivity (intent );
Break;
Case R. id. button4:
// Obtain the MyApp object
MyApp myApp = (MyApp) getApplicationContext ();
MyApp. country = "us ";
MyApp. data. id = 1234;
MyApp. data. name = "UFO ";
Intent = new Intent (TransmitDataActivity. this, MyActivity4.class );
StartActivity (intent );
Break;
Default:
Break;
}
}
}
}

Data class: this class is Serializable, that 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 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 );
// Obtain the String type value
String intentString = getIntent (). getStringExtra ("intent_string ");
// Obtain the Integer value
Int intentInteger = getIntent (). getExtras (). getInt ("intent_integer ");
// Obtain the Data type value
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 passed value on the screen
TextView. setText (sb. toString ());
}
}
In 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 Myactivity3:

Package mobile. android. transmit. data;
Public class MyActivity3 extends Activity {
@ SuppressLint ("NewApi ")
@ 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 );
// Obtain the clipboard object
ClipboardManager clipboardManager = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE );
// Obtain the Base64 encoded string from the clipboard
String base64Str = clipboardManager. getPrimaryClip (). getItemAt (0). getText (). toString ();
// Encode 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 );
// Restore a byte stream to a Data object
Data data = (Data) ois. readObject ();
// Display the Base64 encoded original text 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 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 );
}
}

 

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.