Ultra-smart Android data transmission method and android Data Transmission

Source: Internet
Author: User

Ultra-smart Android data transmission method and android Data Transmission

In Android, data transmission between activities is somewhat complicated. For example, when a complex object is passed through Intent and Bundle, it must be a class that implements the Serializable interface, this makes data transmission between pages inconvenient in many cases. In JavaWeb, It is very practical and convenient for the Session object to transmit data. In Android, can I implement a Session similar to that in JavaWeb?

In fact, it is very simple. The specific implementation process is as follows:

/*////////////////////////////////////// /// // _ OoOoo _ o8888888o 88 ". "88 (| ^_^ |) O \=/O ____/'---'\____. '\\| | //'. /\ |: | // \/_ | -: -|-\ | \-// | \ _ | ''\ ---/'' | \. -\__'-'___/-. /___'.. '/--. --\'.. ___. "" '<'. ___ \_< |> _/___. '> '"". | :'-\'.; '\_/';. '/-': | \\'-. \___\/___/. -'// ======== '-. ____'-. ___\_____/___. -'____. -'=' ^ ^ Buddha bless never BUG Code by duxu0711@163.com /////// //////////////////////////////////////// /// // */package cn. geekduxu. android; import java. util. hashMap; import java. util. map;/*** simulate JavaWeb Session in Android * @ author <a href = "mailto: /duxu0711@163.com "> du Xu </a> */public class Session {private static Session INSTANCE; private Map <String, Object> map; private Session () {map = new HashMap <String, Object> () ;}/ *** get the reference of the Session Object * @ return Session Object */public static synchronized Session getSession () {if (null = INSTANCE) {INSTANCE = new Session ();} return INSTANCE ;} /*** Save the value to the Session * @ param key-key associated with the specified value * @ param value-value associated with the specified key */public void put (String key, object value) {map. put (key, value);}/*** get the object saved in the Session * @ param key returns the key of its associated value * @ return specifies the value mapped by the key; if the ing relationship does not contain the key, null */public Object get (String key) {return map is returned. get (key);}/*** remove an Object from the Session * @ param key the key to remove the Object * @ return Delete Object */public Object remove (String key) {return map. remove (key);}/*** clear Session */public void clear () {map. clear ();}}
In this way, you can simulate the effect of sessions in Java Web in Android, which is much more convenient for data transmission. (The code in the example is not rigorous. If you use it in a program, consider exceptions)


Data transmission in Android Development

A is the parent Activity and B is the Child Activity.
In A, startActivityForResult () and override OnActivityResult ();
When B is about to end, declare intent, put the data in, And then setResult (intent );
Next, the OnActivityResult () method in A is used to obtain the intent, and the data is in the intent.
For details about the parameters, refer to the official API.
Above

In android, how does one use intent to transmit data by passing variables?

Private static final String DATABASE_NAME = "test. db ";
Private static final int DATABASE_VERSION = 1;

Public class DatabaseHelper extends SQLiteOpenHelper {
Public DatabaseHelper (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
}

In this way, you can define the parameters first. You can try again. I am also engaged in android development. If you have any questions, please contact me. My QQ account is 379371398 and I hope to adopt it.


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.