Principle and usage analysis of mutual invocation and transfer parameters between Android activity _android

Source: Internet
Author: User
Tags object serialization serialization sqlite database

The examples in this article describe the mutual invocation and transfer parameters between Android activity. Share to everyone for your reference, specific as follows:

How the activity is invoked between

In the Javaweb program, calls between JSP and JSP are done through redirection, and in Android, the switch between activity and activity is done through intent.

The so-called intent, which is a very important built-in component of Android, can be understood as "what I'm going to do". There are 3 big components in Android: Activity,service, broadcast, and the communication between them is done through the intent, so intent is actually telling everyone between them what to do.

Create your own activity

Mr. Gao's Android handout includes the following:

How do Android's 4 direct components (that is, activity, Service, Intentreceiver, and ContentProvider) communicate with each other? All 4 of these direct components are initiated by Android, and are not initiated between components through a call.

The text is a very insightful one, and it tells the reader (for example, activity) that the activity is Android-initiated, so that programmers have to explicitly tell Android "what's in my app that you're going to start for me", And how to tell Android this information, Android provides us with a androidmanifest.xml file. In this file, you can tell Android these things. This is why we have to configure the configuration file after we have established our own activity, contentprovider, and so on.

The following code is an activity named secondactivity in its own definition, with the following configuration code:

<activity android:name= ". Secondactivity "
     android:label=" secondactivity ">
</activity>

In Android:name, the "." Represents the default package name for the current configuration file.

The switch between the activity

Invoking another activity from one activity is very simple, just create a intent and tell intent where I come from, where to go, and execute this intent. The sample code is as follows:

Intent i = new Intent (testactivity.this,secondactivity.class);
StartActivity (i);

Of course, this will only be able to switch between the activity, if you want to switch at the same time, from the old activity to the new activity to pass some parameters, then in the StartActivity () method, to intent add parameters, by Putextra (STRING,XXX) This set of methods can be implemented.

Putextra (string,xxx) method is similar to map, the key is string type, the value is unjust type, the method is overloaded, the specific reference API, sample code as follows:

Intent i = new Intent (testactivity.this,secondactivity.class);
I.putextra ("UName", "Legend");
I.putextra ("Uage", (short));
StartActivity (i);

Gets the value passed by intent

The old activity passes the value to the new activity, and in the new activity, the Getintent () method is used to receive the passed intent. After the intent is obtained, different types of values can be obtained by means of the Getxxxextra () method. As shown below:

Intent i = getintent ();
String uName = I.getstringextra ("UName");
Short uage = I.getshortextra ("Uage", (short) 0);

The old activity gets the intent after the new activity ends

In development, this is the case, for example, if one of our activity is to invoke a new event, and after the user has completed the new operation, closing the new one, it needs to pass some of the values from the new event to the old activity. This doesn't have to be handled by itself, and Android has written a onactivityresult (int requestcode, int resultcode, Intent data) method to deal with this situation.

First, the intent method in the old acitvity can no longer use the previous startactivity, but to the Startactivityforresult (I, REQUESTCODE1); The first parameter of the method is the intent to be passed, and the second parameter is the request code, which is actually an integer variable, indicating that the method, or the control that initiated the intent, is the main purpose of the Onactivityresult (int Requestcode, int resultcode, Intent data) method, programmed to use. Readers here can understand this without having to delve into it and see the examples. The code here is as follows:

Intent i = new Intent (testactivity.this,secondactivity.class);
I.putextra ("UName", "Legend");
I.putextra ("Uage", (short));
Startactivityforresult (i, REQUESTCODE1);

Second, in the new activity to accept the intent just passed, accept the method as described above, here no longer repeat. After the new acitvity process is complete, the Setresult (Intent i) method needs to be used to set the Intent of the old activity after the new activity ends. Then call the finish () method of the new activity to end the new acitvity. The code is as follows:

Intent resultintent = new Intent ();
Resultintent.putextra ("UName", "Legend2");
Resultintent.putextra ("Uage", (short));
Setresult (RESULTCODE1, resultintent);
SecondActivity.this.finish ();

Finally, rewriting the onactivityresult (int requestcode, int resultcode, Intent data) method in the old activity, the three parameters of the method are not discussed here, I believe that the reader will see that Here are the following code:

if (Requestcode = = REQUESTCODE1) {
     System.out.println ("Requestcode equal");
     if (ResultCode = = secondactivity.resultcode1) {
         System.out.println ("ResultCode equal");
         String uName = Data.getstringextra ("UName");
         Short uage = Data.getshortextra ("Uage", (short) 0);
         Tv.settext ("UName:" +uname+ "Uage:" +uage);
     }


Reference Program

Activitytest

Add:

In intent, you can carry information of type object, and after the reader has consulted the API, you may put it first, in the Putextra () method, and not implement object, but overload the following method: Putextra (String name, Serializable Value), as long as the object implements the serializable interface, it can be added into the intent.

Object serialization and deserialization

When an object is produced, it actually opens up a storage space in memory to facilitate the storage of information.

Object serialization, is an object into a binary data flow of a method, through the serialization of objects can facilitate the implementation of object transmission and storage.

If an object of a class wants to be serialized, the serializable interface must be implemented, but there is no method in the interface, which belongs to a labeled interface, indicating a certain capability.

Serialization and deserialization can be understood as:

Serialization: Program Àobjectoutputstreamà Serialization Object
Deserialization: Serialization of objects Àobjectinputstreamà programs

When serialized, the serialized version can be identified by Serialversionuid.

It is worth noting that serialization stores only the member variables of the object.

The ReadObject () method in ObjectInputStream can read objects.
The WriteObject () method in ObjectOutputStream can write to an object.

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.