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
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
- Get the results of the activity
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics (18)