[Apidemos for Android samples video series] app-activity-receive result

Source: Internet
Author: User
1. Demo description and demonstration

This demo demonstrates how to use startactivityforresult to get a returned result from the started activity. The main lessons learned are as follows:

 

  • Startactivityforresult
  • Functions of Request Code and result code

 

The demo is as follows:

2. Video description
Http://www.eyeandroid.com/thread-10471-1-1.html

3. Demo Analysis

Startactivity can start a new activity. The new activity is relatively independent from the activity that starts it and does not return the results of the activity that calls it. When writing an application, you sometimes need to return results from other activities. Receive result shows this usage.

Startactivityforresult is similar to startactivity. It is important that startactivityforresult can return results from the called activity. Get_code is the request code. When a sub-activity returns results, the request code can be used to process the results returned by the activity.

 
  1. // Definition of the one requestcode we use for processing ing resuls.
  2. Static final private int get_code = 0;
  3. Private onclicklistener mgetlistener = new onclicklistener (){
  4. Public void onclick (view v ){
  5. // Start the activity whose result we want to retrieve.
  6. // Result will come back with Request Code get_code.
  7. Intent intent = new intent (receiveresult. This, sendresult. Class );
  8. Startactivityforresult (intent, get_code );
  9. }
  10. };

When the child activity ends, call setresult to set the returned result. Two activities are involved in the receive result example. receiveresult is the parent activity, sendresult is the user option returned by the child activity, and the returned result is displayed in receiveresult.
The Code returned by sendresult is as follows:

 
  1. Private onclicklistener mcorkylistener = new onclicklistener ()
  2. {
  3. Public void onclick (view V)
  4. {
  5. // To send a result, simply call setresult () before your
  6. // Activity is finished.
  7. Setresult (result_ OK, (new intent (). setaction ("Corky! "));
  8. Finish ();
  9. }
  10. };
  11. Private onclicklistener mviletlistener = new onclicklistener ()
  12. {
  13. Public void onclick (view V)
  14. {
  15. // To send a result, simply call setresult () before your
  16. // Activity is finished.
  17. Setresult (result_ OK, (new intent (). setaction ("Violet! "));
  18. Finish ();
  19. }
  20. };

After a Child Activity returns results, the parent activity processes the results returned by the Child Activity by defining onactivityresult.

 
  1. Protected void onactivityresult (INT requestcode, int resultcode,
  2. Intent data ){
  3. // You can use the requestcode to select between multiple child
  4. // Activities you may have started. Here there is only one thing
  5. // We launch.
  6. If (requestcode = get_code ){
  7. // We will be adding to our text.
  8. ...
  9. }
  10. }

Three parameters in onactivityresult:
Requestcode: The request code when startactivityforresult is called. You can use the request code to distinguish multiple sub-acitifiers.
Resultcode: return value of the sub-activity, which is generally result_ OK (normal) and result_cancelled (canceled ).
Data: return result of activity. In this example, we use the selected Corky or violet.

4. We learned this demo.

1. Usage of startactivityforresult

Http://www.eyeandroid.com/thread-10257-1-1.html

2. Android textview. buffertype instance description

Http://www.eyeandroid.com/thread-9134-1-1.html

Related Article

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.