Using bundles to pass data between activity

Source: Internet
Author: User

Use bundles to transfer data sources between activity activity

public class Sourceactivty extends Activity {
Private Intent Intent=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Intent=new Intent ();
Bundle Mybundelforname=new Bundle ();
Mybundelforname.putstring ("Key_name", "1111");
Mybundelforname.putstring ("Key_age", "22");
Intent.putextras (Mybundelforname);
TextView text= (TextView) Findviewbyid (r.id.test);
Text.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
Intent.setclass (Sourceactivty.this, Targetactivty.class);
StartActivity (Intent);


}
});
}

}

Target Activty

public class Targetactivty extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.intest);
TextView text= (TextView) Findviewbyid (R.ID.TT);
Get Data from intent
Bundle mybundelforgetname=this.getintent (). Getextras ();
String name=mybundelforgetname.getstring ("Key_name");
Text.settext ("Welcome you into:" +name);
}
}

Transfer a service request to the target activity from the source request activity through a intent

SOURCE Activty

Private Intent openwelcomeactivityintent = null;
TextView Text=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Openwelcomeactivityintent = new Intent ();
Set the next activity to open
text= (TextView) Findviewbyid (r.id.test);
Text.setonclicklistener (New Onclicklistener () {
public void OnClick (View v) {
Openwelcomeactivityintent.setclass (Sourceactivty1.this,
Targetactivty1.class);
Startactivityforresult (openwelcomeactivityintent, 1);

}
});

}

@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (Requestcode = = 1) {
if (ResultCode = = result_canceled) {
Settitle ("cancel****");
} else if (ResultCode = = RESULT_OK) {
Bundleshowbundle = Data.getextras ();//Get bundles from the returned intent
Text.settext ("The name get from the second layout:\n" + showbundle.getstring ("MyName"));
}
}
}
}



? The first parameter is the corresponding request code when you open the request intent, you can define it yourself.
? The second parameter is the validation result code returned by the target activity
? The third parameter is the intent that the target activity returns

Target Activty


public class TargetActivty1 extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.intest);
Intentbackintent=new Intent ();
Bundlestringbundle=new Bundle ();
Stringbundle.putstring ("MyName", "Lishiyao");
Backintent.putextras (Stringbundle);
Setresult (RESULT_OK, backintent);//Return activity result code
Finish ();
}

}

Using bundles to pass data between activity

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.