Activity details 2 activity data transmission, activity details

Source: Internet
Author: User

Activity details 2 activity data transmission, activity details

First, you can see:


1. Functions of the Bundle class

The Bundle class is used to carry data. Similar to Map, it is used to store values in the form of key-value Name-value pairs. Compared with Map, it provides various common types of putXxx ()/getXxx () methods, such as: putString ()/getString () and putInt ()/getInt (), putXxx () the getXxx () method is used to get data from the Bundle object. The Bundle actually uses the HashMap <String, Object> type variable to store the value put by the putXxx () method. To put it simply, Bundle is an encapsulated package, which is used to import a packet with Intent values.

2. Two Ways to append data to Intent

The first method is used to add data to Intent in batches:

Intentintent = new Intent ();

Bundle bundle = new Bundle (); // This class is used to carry data

Bundle. putString ("name", "Alice ");

Intent. putExtras (bundle); // append additional data to the intent. data with the original intent will not be lost, but data with the same name as the key will be replaced

The second Writing Method: This writing method is equivalent to the above writing method, but this writing method adds data to Intent one by one, which is convenient to use, you only need to write a small amount of code.

Intent intent = new Intent ();

Intent. putExtra ("name", "XXX ");

So what are the differences between the two methods?

There is no difference at all. When you call the putExtras () method, the passed Bundle is converted to the Intent key value (do not forget that the Intent also reprints data in the key value mode ).

Now let's look at how to extract Intent and Bundle.

The method is very simple. You can directly use this. getIntent () to get the sent Intent, and then call getExtras () on the basis of this Intent to get the Bundle. Then you can get the Bundle if you want it.

For example, String str = bundle. getString ("USERNAME") is the string whose key is "USERNAME", int num = bundle. getInt ("Number") is the integer whose key is "Number.

Objects transmitted between components in android generally implement the Parcelable interface. Of course, you can also use the java Serializable interface. The former is specially designed by android and is more efficient. Next we will implement a Parcelabel.

1. Create a class to implement the Parcelable interface. The specific implementation is as follows:

Public class ParcelableData implements Parcelable {private String name; private int age; public ParcelableData () {name = "guest"; age = 20;} public ParcelableData (Parcel in) {// The order must be the same as that written by writeToParcel. name = in. readString (); age = in. readInt ();} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}@ Override public int describeContents () {// TODO Auto-generated method stub return 0 ;}@ Override public void writeToParcel (Parcel dest, int flags) {// TODO Auto-generated method stub dest. writeString (name); dest. writeInt (age);} public static final Parcelable. creator <ParcelableData> CREATOR = new Parcelable. creator <ParcelableData> () {public ParcelableData createFromParcel (Parcel in) {return new ParcelableData (in);} public ParcelableData [] newArray (int size) {return new ParcelableData [size] ;}};}


2. Send the object using the following method. The Bundle class also implements the Parcelable interface. In android, we use Bundle to encapsulate and transmit data.

Intent intent = new Intent (); intent. setClass (this, SubActivity. class); // directly add // intent. putExtra ("MyData", new ParcelableData (); // Bundle = new bundle (); Bundle. putString ("MyString", "test bundle"); bundle. putParcelable ("MyData", new ParcelableData (); intent. putExtras (bundle); startActivity (intent );



3. the following method for receiving objects.

//ParcelableData parcelableData = getIntent().getParcelableExtra("MyData");  Bundle bundle = getIntent().getExtras();  ParcelableData parcelableData = bundle.getParcelable("MyData");  String testBundleString = bundle.getString("MyString");  Log.v("string=", testBundleString);  Log.v("name=", parcelableData.getName());  Log.v("age=", ""+parcelableData.getAge());  
3. Download DEMO

Activity Code:

Package testbundle; import android. content. intent; import android. support. appCompatActivity; import android. OS. bundle; import android. text. textUtils; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. editText; import android. widget. toast; import java. util. arrayList; import testbundle. entity. person; public class TestBun DleActivity extends AppCompatActivity {EditText editText1; EditText editText2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_test_bundle); editText1 = (EditText) findViewById (R. id. editText1); editText2 = (EditText) findViewById (R. id. editText2);} // submit the selected region and pass the region to TestBundleActivity3 public void submitRegion (Vie W view) {EditText editTextRegion = (EditText) findViewById (R. id. editTextRegion); Intent intent = new Intent (this, TestBundleActivity3.class); String region = editTextRegion. getText (). toString (); if (! TextUtils. isEmpty (region) {intent. putExtra ("region", region); startActivity (intent);} else {Toast. makeText (this, "region cannot be null", Toast. LENGTH_SHORT ). show () ;}/// pass in the TestBundleActivity1 public void calculte (View view) {Intent intent = new Intent (this, testBundleActivity1.class); Bundle bundle = new Bundle (); String first = editText1.getText (). toString (); String second = editTe Xt2.getText (). toString (); if (! TextUtils. isEmpty (first )&&! TextUtils. isEmpty (second) {bundle. putInt ("first", Integer. parseInt (first); bundle. putInt ("second", Integer. parseInt (second); intent. putExtras (bundle); startActivity (intent);} else {Toast. makeText (this, "the value cannot be blank", Toast. LENGTH_SHORT ). show () ;}/// pass the Serializable object to TestBundleActivity2 public void login (View view) {EditText editTextName = (EditText) findViewById (R. id. editTextName); EditText edi TTextCode = (EditText) findViewById (R. id. editTextCode); Intent intent = new Intent (this, TestBundleActivity2.class); Bundle bundle = new Bundle (); String name = editTextName. getText (). toString (); String code = editTextCode. getText (). toString (); if (! TextUtils. isEmpty (name )&&! TextUtils. isEmpty (code) {Person person Person = new person (); Person. setName (name); person. setCode (code); bundle. putSerializable ("person", person); intent. putExtras (bundle); startActivity (intent);} else {Toast. makeText (this, "Name number cannot be blank", Toast. LENGTH_SHORT ). show ();}}}


Finally, the above examples are source and Android worry-free, please go to the app or pea pod download: ApkName = com. shandong. mm. androidstudy. The source code example documentation is exhausted.

Related Article

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: 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.