Startactivityforresult and Setresult

Source: Internet
Author: User

A few days ago my friend asked me about the interaction between the activity, taking advantage of the time to summarize briefly:

1. Start a new activity (B) from an activity (a)

This is usually used more, directly using intent, in activity (A), as follows:

    New Intent (A.This, B.class);    Intent.putextra ("key", "value");    StartActivity (intent);

Here you can pass eight basic data types (Int,string,long,boolean, etc.), as well as bundles (like the map collection).

The parameters to receive the pass in B are as follows:

    String s = getintent (). Getextras (). getString ("key");

You can also use:

  String s = getintent (). Getextras (). getString ("Key", "");

2. In some cases, while activity (A) starts activity (b), we also want B to return a certain data to a, for example, now there is a list containing the user's personal information in a, click Add user information to start B,

After the completion of B, notify a refresh list, using the following:

Start Acitivty (B): Here we do not use startactivity, but with Startactivityforresult, as follows:

        Public Static Final int requestcode = 1;   // declaring global variables       New Intent (A.This, B.class);       Startactivityforresult (Inten T, Requestcode);

Also re-onactivityresult the method in a:

    /*** Requestcode corresponds to Requestcode in Startactivityforresult * ResultCode returned result status, usually three kinds: correct, error, cancel, this is relatively broad, can be defined by themselves * Data returned is usually in*/@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode! =RESULT_OK) {            return; }        if(Requestcode = = 1) {String s= Data.getstringextra ("Key"); }    }

In B:

      New Intent ();      Intent.putextra ("key", "value");      Setresult (RESULT_OK, intent); The first parameter corresponds to the ResultCode intent in Onactivityresult  corresponding to the data

Note here: Setresult must be called before finish ().

Startactivityforresult and Setresult

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.