A method of implementing data transfer between activities and Android

Source: Internet
Author: User
Tags ming

A method of implementing data transfer between activities and Android

This article illustrates the method of data transfer between Android implementation activities. Share to everyone for your reference. The specific analysis is as follows:

First, explain the start and shutdown of activity:

1. StartActivity (Intent Intent); Start activity

Finish (); End Current Activity

2. Startactivityforresult (Intent Intent, int requestcode); Starts an activity with the specified request code Requestcode

finishacitivity (int requestcode); Ends an activity initiated with the Startactivityforresult (Intent Intent, int requestcode) method

If there are multiple activity, then we have to close one of them, then we can set the Requestcode to close the corresponding activity.

Next, we'll talk about data transfer. The problem is this: if we have some data from page A to page b,a, such as user information (username, account number, password, etc.), from a to page B, in B we want to get the data information, how to pass it?

Intent provides a variety of ways to carry additional data information:

Putextras (Bundle data): Put a drug-carrying packet into the intent

Bundle Getextras (): Remove the packets carried in the intent and get a Bundle object

Putextra (String name, Xxx value): Storing the data in the form of a key value in intent to the Key-value

GetXXX (String name): Gets the corresponding data information from the intent according to the key

Putxxx (String key, XXX data): Put the XXX type of data information into the bundle

GetXXX (String key): Get key data information from bundle

Putserializable (String key, Serializable data): Place an object that can be serialized into the bundle

Getserializable (String key, Serializable data): Remove an object that can be serialized from the bundle

With these methods, we are able to deliver data:

The method is as follows:

1. In page A click on the button bn, jump to the page B code as follows:

?

1 2 3 4 5 6 7 8 9 10 11-12 int requestcode = 0; Bn.setonclicklistener (New Onclicklistener ()) {@Override public void OnClick (View source) {Intent intent= new (A.this, B.C LASS); Startactivityforresult (Intent, Inquestcode); Opens the specified activity B and waits for the returned result,//where Inquestcode is the request code}}

2. Add the data in page B, the code is as follows:

?

1 2 3 4 5 6 7 Intent Intent = Getintent (); Gets the intent Intent.putextra ("Data content", "I am Li Ming") corresponding to the currently initiated acitivity B; Bind data information to intent "I am Li Ming" b.this.setresult (1, intent); Set the result code and set the activity B.this.finish () returned after the end;

3. To obtain the data information in a, the code is as follows:

In page A, in order to get the result returned in B, we should rewrite the Onactivityresult () method in Page A, which will be invoked when B returns the result:

?

1 2 3 4 5 6 7 8 9 10 11-12 String MyData; @override public void Onacitivityresult (int requestcode,int resultcode,intent Intent)//override this method to obtain the result returned by B {if ( Requestcode==0&&resultcode==1) {Bundle data = Intent.getextras (); String myresultdata = data.getstring ("Data content"); So myresultdata is the data message we're going to pass from B}

Summing up, by using the methods in intent we can achieve data transfer between different activity, by setting the identifier and result code, we can selectively open and close different activity, which is the data transfer between the activity.

I hope this article will help you with your 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.