Data transmission between activities in Android

Source: Internet
Author: User

Effect: There are two activities: A and B, which use bundle to encapsulate data in a to pass data to B, and then use startactivityforresult to modify and return data in B.

Layout of the first activity, such as main. xml:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" 3 Android: Orientation = "vertical" 4 Android: layout_width = "fill_parent" 5 Android: layout_height = "fill_parent"> 6 <textview 7 Android: Id = "@ + ID/text" 8 Android: layout_width = "fill_parent" 9 Android: layout_height = "wrap_content" 10 Android: TEXT = "Welcome to my blog:"/> 11 <Button 12 Android: Id = "@ + ID/edit_btn" 13 Android: layout_width = "fill_parent" 14 Android: layout_height = "wrap_content" 15 Android: text = "edit"/> 16 </linearlayout>

The effect is as follows:

At the beginning, it was just a textview that showed welcome to my blog: Now you need to transfer the data in textview to the second activity when you click the edit button, and then return the edited data.

In the first activity, bundle is used to encapsulate and transmit data, and the gettext () method is used to obtain text from textview. The core code is as follows:

 1         text = (TextView)findViewById(R.id.text); 2         editBtn = (Button)findViewById(R.id.edit_btn); 3         editBtn.setOnClickListener(new OnClickListener(){ 4             @Override 5             public void onClick(View v) { 6                 // TODO Auto-generated method stub 7                 Bundle bundle = new Bundle(); 8                 bundle.putString("text_view", text.getText().toString()); 9                 Intent intent = new Intent(DataTransActivity.this, SecondActivity.class);10                 intent.putExtras(bundle);11                 startActivityForResult(intent, 0);12             }13         });

In Android, startactivityforresul (intent, int requestcode) is mainly used to return data. Here, you must note that requestcode must be greater than or equal to 0, which is used for identification during callback.

Okay, the data is sent out. How can I receive the data in the second activity? Open the layout file first:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" 3 Android: Orientation = "vertical" 4 Android: layout_width = "fill_parent" 5 Android: layout_height = "fill_parent" 6> 7 <edittext 8 Android: Id = "@ + ID/edittext" 9 Android: layout_width = "fill_parent" 10 Android: layout_height = "300dp"/> 11 <linearlayout 12 Android: Orientation = "horizontal" 13 Android: layout_width = "fill_parent" 14 Android: layout_height = "wrap_content"> 15 <button 16 Android: Id = "@ + ID/submit" 17 Android: layout_width = "0dp" 18 Android: layout_height = "wrap_content" 19 Android: layout_weight = "1.0" 20 Android: text = "OK"/> 21 <button 22 Android: Id = "@ + ID/cancel" 23 Android: layout_width = "0dp" 24 Android: layout_height = "wrap_content" 25 Android: layout_weight = "1.0" 26 Android: TEXT = "cancel"/> 27 </linearlayout> 28 </linearlayout>

As shown in the following figure, the text http://www.cnblogs.com/sense7/is an external addition. The data transmitted from the main activityis welcome to my blog: the "OK" button will return the modified data, and the "cancel" button will return the main activity and prompt that no.

First, it receives data transmitted by the main activity:

1         Bundle bundle = getIntent().getExtras();2         editText.setText(bundle.getString("text_view"));

Next, click the OK button to encapsulate and return the modified data:

1 submit. setonclicklistener (New onclicklistener () {2 @ override 3 Public void onclick (view v) {4 // todo auto-generated method stub 5 // instantiate bundle, set the parameter 6 bundle = new bundle (); 7 bundle. putstring ("edit_text", edittext. gettext (). tostring (); 8 secondactivity. this. setresult (result_ OK, secondactivity. this. getintent (). putextras (bundle); 9 secondactivity. this. finish (); 10} 11 });

Code for clicking the cancel button:

1         cancel.setOnClickListener(new OnClickListener(){2             @Override3             public void onClick(View v) {4                 // TODO Auto-generated method stub5                 SecondActivity.this.setResult(RESULT_CANCELED);6                 SecondActivity.this.finish();7             }8         });

Setresut (INT resultcode, intent) returns the result. resultcode is used to differentiate different returned results. result_ OK and result_canceled are built-in parameters for ease of use, in intent, the modified data can be encapsulated and returned to the main activity. Another point is to call the finish () method of the current activity.

The following describes the correspondence between the onactivityresult (INT requestcode, int resultcode, and intent data) method, requestcode, and startactivityforresul (intent, int requestcode) in the main activity, resultcode is the passed value, and data is the return value. The code of the rewrite method is:

1 @ override 2 protected void onactivityresult (INT requestcode, int resultcode, intent data) {3 // todo auto-generated method stub 4 super. onactivityresult (requestcode, resultcode, data); 5 If (resultcode = result_ OK) {6 bundle = data. getextras (); 7 text. settext (bundle. getstring ("edit_text "). tostring (); 8} else if (resultcode = result_canceled) {9 toast. maketext (this, "No changes made", toast. length_short ). show (); 10} 11}

Click "OK" to modify the text, and the returned result is as follows:

Click Cancel to return the following information:

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.