Call Startactivityforresult to start activity, return the current page does not respond to the problem (with the activity carrying parameters process)

Source: Internet
Author: User

Recently in the project encountered such a problem, the original activity is not for me to write, I want to change back to the activity carrying parameters. After I changed it, I found I couldn't call Onactivityresult. Debugging is no problem, I also use the finish function at the end of the activity. In this case, you will not be careful to find the manifest configuration activity statement. Here's a question that doesn't respond.

First, the Manifest configuration of the starting mode about

Activity is related to how Manifest is configured to start, do not configure boot mode, android:launchmode= "Singletask". The reason is to jump to the page in Androidmanifest.xml I set the android:launchmode= "Singletask", because the need to transfer the value of Activity does not allow the setting of this property or SingleInstance, Or can only be set to standard mode, or Onactivityresult () will be called directly after Startactivityforresult (). In addition, the Requestcode value must be greater than or equal to 0, otherwise, the startactivityforresult becomes the startactivity.

Second, press the return key, also call finish this function.

Finish () must be called after Setresult () in B, then go back to A,a to automatically call Onactivityresult ()
If you press back directly, it will not be adjusted.

                Startactivityforresult (intent,100); This sentence starts activityintent intentsend = new Intent ();    When returned, the parameters are set bundle Sendbundle = new bundle (); Intentsend.putextras (Sendbundle); Setresult (RESULT_OK, intentsend); Finish ();//Return key Call public boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method stubif (keycode = = Keyevent.keycode_back) {finish ();} return true;}

three, the following general process

The process is aactivity enters bactivity, enters and returns all carry data, returns after refreshing aactivity. There must be a sign that startactivityforresult (Newintent, 12); This sentence is obvious, starting a new activity with results.

1, itent aactivity enter bactivity, use bundle carry data, flag 12

Intent newintent = new Intent (); Newintent.setclass (this, tactivity.class); Bundle Sentbundle = new Bundle (), sentbundle.putstring ("Template", String.Format ("%.4f", 555)), Newintent.putextras ( Sentbundle); Startactivityforresult (Newintent, 12);
2, access to the new bactivity after the data carried on the

Bundle bundle = This.getintent (). Getextras (); if (bundle==null) {return;}                             Bundle.getstring ("Template");

3, Bactivity return, carry data to aactivity
Intent intentsend = new Intent (); Bundle bundlesend = new Bundle (), bundlesend.putstring ("Template", "Data"), Intentsend.putextras (bundlesend); Setresult ( RESULT_OK, Intentsend); finish ();
4. Accept data when Aactivity returns
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent datareceive) {if (datareceive = = null) RE Turn;if (Requestcode = =) {Bundle bundlereceive = Datareceive.getextras (); if (bundlereceive = = null)  return; Bundlereceive.getstring ("Template");} Super.onactivityresult (Requestcode, ResultCode, datareceive);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Call Startactivityforresult to start activity, return the current page does not respond to the problem (with the activity carrying parameters process)

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.