About Activity.startactivityforresult (Intent, int) using

Source: Internet
Author: User

The first is to call Setresult (int, Intent) in OnPause () in the activity after startup to set the returned Intent, but onactivityresult in the returned activity (Int,int, Intent) The resulting Intent is a null value of NULL.

This is the boot code

Findviewbyid (R.id.button1). Setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO Auto-generated method Stubintent Intent = new Intent (mainactivity.this, mainactivity2.class); Bundle Sendbundle = new Bundle (), sendbundle.putcharsequence ("str", "from Mainactivity.class"); Intent.putextra ("    Sendbundle ", Sendbundle); Startactivityforresult (intent,0x1001); }}); LOG.I (TAG, "oncreated");
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method Stubsuper.ona Ctivityresult (Requestcode, ResultCode, data); if (data!=null) {String str = Data.getstringextra ("return"); LOG.I (tag,str+ "" +requestcode+ "" +resultcode); if (resultcode==0x1001) {TextView TextView = (TextView) Findviewbyid ( R.ID.TEXTVIEW1); Textview.settext (str);}} ELSE{LOG.I (TAG, "data is null");}}

Above is the result returned by the callback method override

The following is the wrong setresult (int, Intent) code

protected void OnPause () {Intent Intent = new Intent ();    Intent.putextra ("Return", "Return Me"); Setresult (0x1001, intent);}

After the reason is found, modify the above code to OnCreate (Bundle), add a button and append the onclick (View)

Findviewbyid (R.id.button2). Setonclicklistener (New Onclicklistener () {public void OnClick (View v) {Intent Intent = new in  Tent (); Intent.putextra ("Return", "Return Me"); Setresult (0x1001, intent); finish (); The key here is to pass the intent back to the activity that started it and close the current activity});


About Activity.startactivityforresult (Intent, int) using

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.