Android Basics (18)

Source: Internet
Author: User

The return value of the previous page

    • The return value of the previous page
      • Brief introduction
      • Body
      • Extended Reading

Target group: Android Beginner without foundation
Knowledge Points: The use of Onactivityresult methods
Target: Receives the return value of the previous interface and displays it in the current page

Brief introduction
    • Onactivityresult's description

    • Description of the Requestcode and ResultCode parameters

Body

In the previous article, we learned how to pass data to a new page, let's learn how to receive the data returned from the new page:

1. Start a new page code as follows:

  new Intent(this, SecondActivity.class);  int requestCode=100;  startActivityForResult(intent, requestCode);
    • Requestcode is the request code, and when the returned intent parameter is received, the callback method Onactivityresult provides the request code to correctly identify the result and determine how to handle it.

    • Note that the method to start a new page call here is Startactivityforresult rather than startactivity

2. The corresponding code in the Secondactivity page is as follows:

  Intent intent = new Intent();  intent.putExtra("num""123");  finish();  setResult(RESULT_OK, intent);
    • RESULT_OK is the result code, and when the returned intent parameter is received, the callback method Onactivityresult provides the result code to correctly identify the result and determine how to handle it.

3. After returning to the first page, the corresponding code is as follows:

@OverrideprotectedvoidonActivityResult(intint resultCode, Intent data) {    // 发起新页面请求时的请求码    if100) {        // 确认结果码        if (resultCode == RESULT_OK) {            //上一页面传递的数据            String num = data.getStringExtra("num");            tv_num.setText(num);        }    }}
    • From the page logic, it is recommended to Judge Requestcode and ResultCode, but this is not necessary.
Extended Reading
    1. Get the results of the activity

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

Android Basics (18)

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.