Go deep into Android [8]-data transmission between activities

Source: Internet
Author: User
Data transmission between activities

With some knowledge of Android, it is not difficult to find that androidProgramThe UI framework is similar to the web page concept. Every component used to render a page,ActivityAnd are independent from each other. They are scheduled and integrated through the core of the system, and communicate with each other throughIntentMechanism.
Each architecture has its own advantages and disadvantages. Of course, Android cannot be detached and refined. Because of the loose coupling between activitiesReuseThe mash-up method can effectively improve the development efficiency. On the other hand, because the activity is too independent, data sharing between them becomes a troublesome task.
Message-Based Transmission

Data transmission between the most standard activities is through the intentExtraObject. For example, if you get a piece of text information from user input on Activity A and want to put it on Activity B to display and send it, this is a very feasible method, it uses intentPutextraInterface, put the character information entered by the user into the intent in the form of key/value, and transmit it to the activity B.

As shown in, the transfer from A to B seems to be a direct connection, but in fact, intent is to analyze and schedule through the system core layer, this operation spans the process boundary, naturally, the data is serialized and deserialized, instead of passing through a pointer.

The benefits of message-like transmission mode are not mentioned. Let's talk about the following:
    • First,Big DataIt is just a cup of equipment. It is impossible for a piece of M data to be transferred back and forth. Who is responsible for the slow death;
    • In addition, activities are maintainedLinear RelationshipWhen I want to upload a piece of data from the end of the team to the head of the team, I will put all the activities in the middle after my own hardships, obviously, they may not need this data and have to put it in their hands to marry others;
    • In addition, message-based transmission will generate several copies of the same data. Sometimes, this is very good and has no side effects. You do not need to look at others' faces when you play your own games, but sometimes, you just need to modify the data and synchronize it;
    • Finally, write serializationCodeIt's really boring. If the code is a little complicated, you have to write a serialized interface by yourself. The whole process is thankless.
External Storage-based transmission since Beasts It's so lucky to have a hand. Naturally, the idea is to find a place where a puts the data and tells B the address information. If B needs it, just pull it out as shown in the figure. You can choose the place where the things are stored. External Storage . In Android, some convenient classes and modules are preset to better support access to different types of data. You can select Preference It is equivalent to setting files in the traditional sense. Preference provides some access interfaces based on key/value, which can be placed with some simple basic data or derived Parcelable Interface object. A good application scenario is Cookie Storage . You get a cookie on the login interface. You can throw it into preference. Whoever wants it will never go back and forth. Preference is suitable for small data and setting information. If big data is used, you can consider using Database . In Android SQLite , Related classes, stacked in Android. Database Name Space, self-check, no need to repeat.

In Android, the database is private. If you want to shareUsed by third-party components, You need to useContentprovider. For example, if you use the recorder component of the system to instantly produce an audio message, it does not return the horrible recording data, but returnsUriIt indicates the address information of the data in contentprovider, and takes the URI to get the data.

Of course, if you are calm enough, you can also use the red fruit File . If the file exists in the private directory of the mobile phone, you can use it internally and put it on the SD card to share all applications.

The advantages and disadvantages of External Storage-based data transmission are obvious. It solves the problem of complicated intent transmission paths and is not conducive to the transmission of large volumes of data.Efficiency hazards, ComplicatedProgramming Model. In the face of external storage, developers must consider efficiency issues. In many cases, multithreading will be put on the agenda. In this way, it will not work if you don't have to worry about it. Service-based transmission

Since the external environment is too slow, it is better to solve the problem at the memory level. At this time, you may need to come up with one of the four Android components.Service. The purpose of the service design is to provide some background services and data access, which can also be part of its responsibilities.

The Service provides a direct connection mechanism to call the activity. Bindservice Method, establish a data path with the target service to obtain Ibinder . In this way, you can call remote methods and transmit data through the IPC model provided by Android.

As shown above, this mode can solve certain problems, but for the service, it is too small to use, service expertise, not in data or logic. For data transmission, the service is still a bit heavy, not only is there a connection that consumes energy, but transmission through IPC is also hard enough to write. As a component, the Service may die at any time. You still have to work hard to process data persistence, which outweighs the loss.

Transfer Using Application

Well, if you need to haveMemory Object, A suitable way is to throw themApplication. Application is a sub-class of context. It will get up before any component of the entire application. Its Life Cycle runs through the entire life journey of all components of the application. Therefore, the objects in it will not be processed.

In the activity, you can use Getapplication Interface to obtain the reference of the Application object at any time. It is the most suitable place to store and process some global objects.

Of course, do not over-use good things. As you can imagine, due to the long life cycle of the application, the objects referenced on it have never been released. If you treat it as a waste, everything is thrown in, polluting the environment, and confusing logic is not mentioned. The abuse of memory resources alone is enough to be a great deal of guilt. Therefore, if the data does not really need to be used globally, do not place it in it. If the data is too large, do not load it all out. It is necessary to reasonably use storage devices such as databases.
Conclusion

In some special cases, you can consider using some special methods. For example, you can call getparent between sub-activities to obtain the reference of the parent activity, to access the objects in the period, Cloud. Let's not mention the niche situations.
I believe that all coder is well aware of these concepts, and how to deal with such data is like a mirror. I just put an android coat on them, so that the android coder can quickly find their favorite weapon, cut down the rock and attack the city.

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.