Android new window and value transfer

Source: Internet
Author: User

1. Create a new activity, such as Activity2, in the manifest file Androidmanifest.xml in the application node to add another form:

.................

</activity>
<activity android:name= ". Ortheractivity" android:label= "New Window"/>
</application>

2, in the main program:

Intent Intent = new Intent (mainactivity.this, Activity2.class);

Opens a new window. Parameters: Main window, called window

Bundle bundle = new bundle ();//bundle to achieve data transfer:
Bundle.putstring ("Key1", "value1");//Key1 for name, value1 for value
Bundle.putstring ("Key2", "value2");
Bundle.putint ("KeyInt1", 100);
Intent.putextras (bundle); Data transfer

If you do not want the child window to return data:
StartActivity (Intent);

If you need a child window to return data:

(1), Startactivityforresult (Intent, Requestcode);

Note here thatRequestcode is the ID flag of the child window and must be greater than 1, otherwise the callback function Onactivityresult not respond!

(2), implement the callback function Onactivityresult:

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
.....

}

The main window has only one callback function, and if several child windows are called, they need to be distinguished by Requestcode. The ResultCode is the return value of the Subwindow, which can be used for subsequent judgments.

3, the bundle is still used in the new window to get the data:
Bundle bundle = This.getintent (). Getextras ();
String S1 = bundle.getstring ("Key1");
String s2 = bundle.getstring ("Key2");
int I=bundle.getint ("keyInt1");

4, how do I return to the main window?

Intent intent=new Intent ();
Bundle Bunle=new Bundle ();
Bunle.putstring ("Key1", "This is the return data of the child window.");
Intent.putextras (Bunle);
SubActivity.this.setResult (RESULT_OK, intent);
SubActivity.this.finish ();
Setresult (RESULT_OK, intent); Set the return result
Finish (); Close child window, otherwise data cannot be returned

5, reference

Http://blog.sina.com.cn/s/blog_63d409eb01017dnn.html

Transferred from: http://blog.sina.com.cn/s/blog_3ecf39a70101axmo.html

Android new window and value transfer

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.