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