In Android, what is the difference between intent and bundle in passing values?

Source: Internet
Author: User

/**

* Test the difference between using intent to pass a value directly in Android and using bundle to pass a value.
*
* You are welcome to ask questions.
* By garretly
* Mail garretly@gmail.com
*/

 

I went around EOE a while ago and saw someone asking me what is the difference between intent and bundle.

Since intent can pass the value, why do we still create a bundle to pass the value?

 

For example, I want to jump to interface B or interface C from interface.
In this case, I need to write two intents. If you want to transfer the value involved, your intent will write the method of adding the value twice. If I use one bundle to directly store the value first is it more concise to store it in intent?

Public Class A extends activity {
Private button BTN;
Private textview TV;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. );

BTN = (button) findviewbyid (R. Id. btn1 );
TV = (textview) findviewbyid (R. Id. TV1 );

TV. settext ("activity ---- ");

BTN. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
Next ();

}
});

}
Private void next (){
Intent I = new intent ();
I. setclass (this, B. Class );
I. putextra ("int", 1000 );
I. putextra ("string", "test ");
I. putextra ("char", 'C ');
Startactivity (I );
Finish ();
}

}

 

Public Class B extends activity {
Private button BTN;
Private textview TV;

Int temp1;
String temp2;
Char temp3;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. B );

BTN = (button) findviewbyid (R. Id. btn2 );
TV = (textview) findviewbyid (R. Id. TV2 );

Geti (getintent ());

/**
* Why write view. onclicklistener () here ()
* Because onclicklistener () is called, multiple types of onclicklistener are available.
* () Object, so I will not use onclicklistener through the package here
* () Object. Instead, you can add a view before the object to inform the compiler of the onclicklistener () object I am calling.
*/
BTN. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
Next ();

}
});

}

// Obtain data from
Private void Geti (intent I ){
// Int temp1 = I. getintextra ("int", 0 );
// String temp2 = I. getstringextra ("string ");
// Char temp3 = I. getcharextra ("char", 'A ');
Temp1 = I. getintextra ("int", 0 );
Temp2 = I. getstringextra ("string ");
Temp3 = I. getcharextra ("char", 'A ');
System. Out. println (temp1 );
System. Out. println (temp2 );
System. Out. println (temp3 );

TV. settext ("activity ---- B" + "/N" +
"Int =" + temp1 + "/N" +
"String =" + temp2 + "/N"
+ "Char ========" + temp3 );
}

/**
* To transfer the value from A to C, you need to change temp1 temp2 temp3 to a global variable and enter some attributes in B.
*/
Private void next (){
Intent I = new intent ();
I. setclass (this, C. Class );
I. putextra ("int", temp1 );
I. putextra ("string", temp2 );
I. putextra ("char", temp3 );
// At the same time, I want to pass some attributes of B to C
I. putextra ("Boolean", true );
Startactivity (I );
Finish ();
}

}

 

Public Class C extends activity {
Private button BTN;
Private textview TV;

Int temp1;
String temp2;
Char temp3;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. C );

BTN = (button) findviewbyid (R. Id. btn3 );
TV = (textview) findviewbyid (R. Id. TV3 );
// Getintent () is the intent object from which the activity is started.
Geti (getintent ());


BTN. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
Next ();

}
});

}
// Obtain data from C
Private void Geti (intent I ){
// Int temp1 = I. getintextra ("int", 0 );
// String temp2 = I. getstringextra ("string ");
// Char temp3 = I. getcharextra ("char", 'A ');
Temp1 = I. getintextra ("int", 0 );
Temp2 = I. getstringextra ("string ");
Temp3 = I. getcharextra ("char", 'A ');
Boolean temp4 = I. getbooleanextra ("Boolean", false );
TV. settext ("activity ---- C" + "/N" +
"Int =" + temp1 + "/N" +
"String =" + temp2 + "/N" +
"Char =" + temp3 + "/N" +
"Boolean =" + temp4
);
}

Private void next (){
Intent I = new intent ();
I. setclass (this, Com. garretly. B. A. Class );
Startactivity (I );
Toast. maketext (getapplicationcontext (), "test bundle", Toast. length_long). Show ();
Finish ();
}

}

In another example, if I have activity A, B, C;
Now I want to pass the value through a through B to C
How do you say that if intent is used, the A-B first writes it again and then retrieves it in B, then inserts the value into intent, and then jumps to C tired?
If I use bundle in A, I will pass the bundle to B and then transfer it to C in B.
In this case, a new key-value can be added to the bundle object in B, which can also be obtained in C.

 

 

Public Class A extends activity {
/**
* In this class, we use bundle to transmit values.
*/
Private button BTN;
Private textview TV;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. );

BTN = (button) findviewbyid (R. Id. btn1 );
TV = (textview) findviewbyid (R. Id. TV1 );

TV. settext ("activity ---- ");

BTN. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
Next ();

}
});

}
Private void next (){
Intent I = new intent ();
Bundle B = new bundle ();
I. setclass (this, B. Class );
B. putint ("int", 1000 );
B. putstring ("string", "test ");
B. putchar ("char", 'C ');
I. putextras (B );
Startactivity (I );
Finish ();
}

}

 

Public Class B extends activity {
Private button BTN;
Private textview TV;
Bundle B = new bundle ();
Int temp1;
String temp2;
Char temp3;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. B );

BTN = (button) findviewbyid (R. Id. btn2 );
TV = (textview) findviewbyid (R. Id. TV2 );

Geti (getintent ());

/**
* Why write view. onclicklistener () here ()
* Because onclicklistener () is called, multiple types of onclicklistener are available.
* () Object, so I will not use onclicklistener through the package here
* () Object. Instead, you can add a view before the object to inform the compiler of the onclicklistener () object I am calling.
*/
BTN. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
Next ();

}
});

}

// Directly use bundle to obtain data from
Private void Geti (intent I ){
B = I. getextras ();
Temp1 = B. getint ("int", 0 );
Temp2 = B. getstring ("string ");
Temp3 = B. getchar ("char", 'A ');

TV. settext ("activity ---- B" + "/N" + "Int =" + temp1 + "/N" + "string ="
+ Temp2 + "/N" + "char ======" + temp3 );
}

/**
* To transfer the value from A to C, change temp1 temp2 temp3 to the global variable.
* You need to fill in some attributes in B.
*
* We will use bundle directly this time.
*/
Private void next (){
Intent I = new intent ();
I. setclass (this, C. Class );
B. putboolean ("Boolean", true );
I. putextras (B );
// Here, a test is to put B from a in intent for transmission.
I. putextra ("bundle", B );
Startactivity (I );
Finish ();
}

}

 

 

 

Public Class C extends activity {
Private button BTN;
Private textview TV;

Int temp1;
String temp2;
Char temp3;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. C );

BTN = (button) findviewbyid (R. Id. btn3 );
TV = (textview) findviewbyid (R. Id. TV3 );
// Getintent () is the intent object from which the activity is started.
Geti (getintent ());

BTN. settext ("End test ");
BTN. setonclicklistener (New View. onclicklistener (){

@ Override
Public void onclick (view v ){
Next ();

}
});

}
// Obtain data from C
Private void Geti (intent I ){
Bundle B = new bundle ();
B = I. getextras ();
Bundle b2 = I. getbundleextra ("bundle ");
Temp1 = B. getint ("int", 0 );
Temp2 = B. getstring ("string ");
Temp3 = B. getchar ("char", 'A ');
Boolean temp4 = B. getboolean ("Boolean", false );
TV. settext ("activity ---- C" + "/N" +
"Int =" + temp1 + "/N" +
"String =" + temp2 + "/N" +
"Char =" + temp3 + "/N" +
"Boolean =" + temp4 + "/N" +
"B2 =" + b2.tostring () // The data in B2 is no longer retrieved. What do you mean? But the size of the obtained data depends on the underlying code.
);
}

Private void next (){
Toast. maketext (this, "test ended", Toast. length_long). Show ();
Finish ();
}

}

 

 

 

 

Integration Project

Http://download.csdn.net/source/3152096

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