Basic Android: page Jump and value passing (activity jump and value passing)

Source: Internet
Author: User

An Android ApplicationProgramThere is rarely one activity object. It is a basic requirement to jump between multiple activities and pass values to each other.

Here we will talk about several methods of page Jump and value passing in Android!

Activity jump and value passing mainly connects multiple activities through the intent class and transmits data through the bundle class.

The most common and common page JumpCode, Very simple, as follows:

View plaincopy to clipboardprint?
Intent intent = new intent (A. This, B. Class );
Startactivity (intent );
 

You can also write as follows:

View plaincopy to clipboardprint?
Intent intent = new intent ();
Intent. setclass (A. This, B. Class );
Startactivity (intent );
 

With these two sentences, you can jump from page a to page B. (A and B inherit from activity)

Sometimes, data needs to be transferred when the page is redirected. How can this problem be solved?

If the data volume is small, for example, you only need to pass a name, then you only need to add "intent. putextra (" name "," feng88724 ");", the Code is as follows:

View plaincopy to clipboardprint?
Intent intent = new intent ();
Intent. setclass (A. This, B. Class );
Intent. putextra ("name", "feng88724 ");
Startactivity (intent );
 

If there is a large amount of data, you need to use the bundle class. The Code is as follows: (for more information, see annotations)

View plaincopy to clipboardprint?
Intent intent = new intent (A. This, B. Class );

/* Use the bundle object to store the data to be transferred */
Bundle bundle = new bundle ();
/* Character, String, Boolean, byte array, floating point, and so on can all be passed */
Bundle. putstring ("name", "feng88724 ");
Bundle. putboolean ("ismale", true );

/* Assign the bundle object assign to intent */
Intent. putextras (bundle );

Startactivity (intent );
 

We are talking about how to perform page Jump and data transmission. How should we receive data on another page B?

On page A, objects are encapsulated by bundle. On page B, encapsulated data is also unbound by bundle. You can use the "getintent (). getextras ()" method to obtain the bundle and then obtain data from the bundle. You can also use the "this. getintent (). getstringextra (" name ");" method to directly obtain data from intent.

Code for getting data from bundle:

View plaincopy to clipboardprint?
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
/* Load the page */
Setcontentview (R. layout. Main );

/* Get the bundle object in intent */
Bundle bundle = This. getintent (). getextras ();

/* Obtain the data in the bundle. Pay attention to the type and key */
String name = bundle. getstring ("name ");
Boolean ismale = bundle. getboolean ("ismale ");

}

Sometimes, after a page Jump, you need to return to the previous page and retain the user's previous information. What should you do at this time?

After the page jumps, the previous activity has been destroy. To return and display data, You must wake up the previous activity and call a method to obtain and display data.

To achieve this effect, perform the following steps:

1. When you jump from page a to page B, you cannot use the "startactivity ()" method, but the "startactivityforresult" method.

2. In the activity on page A, you must override the onactivityresult method.

 
View plaincopy to clipboardprint?
@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){

Switch (requestcode ){
Case result_ OK:
/* Obtain the data from page B and display it to the screen */
Bundle bundle = data. getextras ();

/* Obtain the data in the bundle. Pay attention to the type and key */
String name = bundle. getstring ("name ");
Boolean ismale = bundle. getboolean ("ismale ");
}
}
 

3. Add a return button on page B and write the following code in the event:

 
View plaincopy to clipboardprint?
/* Return the result to the previous activity */
B. This. setresult (result_ OK, intent );
/* End the activity */
B. This. Finish ();
 

This is the case.

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.