In Android, intent is used to switch the form, and the details of passing values and custom class objects are described.

Source: Internet
Author: User

From http://blog.csdn.net/newflypig/article/details/6246705

 

 

In Android, intent objects are responsible for switching between various activity windows, and at the same time, they are more responsible for data transmission.

 

Generally, the following code uses the intent object for simple window switching:

View plaincopy to clipboardprint?
  1. Intent I = new intent (); // creates an intent object.
  2. I. setclass (login. This, maintable. Class); // The first parameter is the current activity object, and the second parameter is the class name to be switched out.
  3. Login. This. startactivity (I); // The current activity is switched. At this time, the current activity is switched to sleep state and no CPU is used, but snapshots are left in Ram.

Intent I = new intent (); // create an intent object I. setclass (login. this, maintable. class); // The first parameter is the current activity object, and the second parameter is the class name login to be switched out. this. startactivity (I); // The current activity is switched. At this time, the current activity is switched to sleep state and the CPU is no longer used, but snapshots are left in Ram.

 

If you want to switch between two forms and pass some basic parameters to the second form, you need to put the key-value pair in the intent object

The Code is as follows:

View plaincopy to clipboardprint?
  1. Intent I = new intent ();
  2. I. setclass (login. This, maintable. Class );
  3. I. putextra ("key", "value"); // a group of key-value pairs are added to the intent
  4. Login. This. startactivity (I );

Intent I = new intent (); I. setclass (login. this, maintable. class); I. putextra ("key", "value"); // a key-Value Pair login is added to the intent. this. startactivity (I );

 

In the second form, you need to extract the key-value pair from the intent object and use the following statement:

 

View plaincopy to clipboardprint?
  1. String value = This. getintent (). getcharsequenceextra ("key ");

String value = This. getintent (). getcharsequenceextra ("key ");

 

In this way, simple string parameters are passed and retrieved. If you want to implement custom class objects to be passed between two forms, it is actually very simple, you only need to implement the serializable interface of the custom class. This interface is purely a flag and does not require you to implement any function. classes that implement this interface can be between programs and threads, value Transfer between network communication. The following is an example:

View plaincopy to clipboardprint?
  1. @ Suppresswarnings ("serial ")
  2. Public class user implements serializable {// The user class implements the serializable interface.
  3. Private string ID;
  4. Private string name;
  5. Private string department;
  6. Private Boolean pow_freenumber;
  7. Private Boolean pow_report;
  8. Public user (string ID ){
  9. This. ID = ID;
  10. }
  11. }

@ Suppresswarnings ("serial") public class user implements serializable {// The user class implements the serializable interface private string ID; private string name; private string Department; private Boolean pow_freenumber; private Boolean pow_report; public user (string ID) {This. id = ID ;}}

 

I have designed a user class. The setter and getter functions are pasted out for a long time. In database programming, we call this class vo.

After the serializable interface is implemented, we can push the objects of this class into intent.

 

View plaincopy to clipboardprint?
  1. Intent I = new intent ();
  2. I. setclass (login. This, maintable. Class );
  3. User user = new user ("001", "ding"); // Test Data
  4. User. setdepartment ("Business Support Center ");
  5. User. setpow_freenumber (FLAG );
  6. User. setpow_report (true );
  7. I. putextra ("user", user );
  8. Login. This. startactivity (I );
  9. Login. This. Finish ();

Intent I = new intent (); I. setclass (login. this, maintable. class); User user = new user ("001", "ding"); // test data user. setdepartment ("Business Support Center"); User. setpow_freenumber (FLAG); User. setpow_report (true); I. putextra ("user", user); login. this. startactivity (I); login. this. finish ();

 

In the second form, we can use the following sentence to retrieve the entire user object.

 

 

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.