The activity instance starts the activity and returns the result

First, let's show you the effect diagram:

1 Introduction

If you want to get the data returned after the newly opened activity is closed in the activity, you need to use the startactivityforresult (intent, int requestcode) method provided by the system to open the new activity. After the new activity is closed, it will return data to the previous activity. In order to get the returned data, you must rewrite onactivityresult in the previous activity (int requestcode, int resultcode, intent data) method.

When the new activity is closed, the data returned by the new activity is transferred through intent. The Android platform will call the onactivityresult() method of the previous activity, and pass in the intent storing the returned data as the third input parameter. The data returned by the new activity can be retrieved by using the third input parameter in the onactivityresult() method.

2 setResult

Use the startactivityforresult (int, int requestcode) method to open a new activity. Before closing the new activity, you need to return data to the previous activity. You need to use the setresult (int resultcode, int data) method provided by the system to implement:

The first parameter value of setresult () method can be defined according to business needs. The result used in the above code_ OK is a constant defined by the system activity class with a value of - 1. The code fragment is as follows:

Note: when you click the "open new activity" button, you will jump to the "I am a newly opened activity" page;

Click the "close" button to close the current page, jump to the "I am an old activity" page, and pass the result parameter to the previous activity

3 function of request code

To open a new activity using the startactivityforresult (intent, int requestcode) method, we need to pass in a request code (second parameter) for the startactivityforresult () method. The value of the request code is set by yourself according to business needs and is used to identify the source of the request. For example, an activity has two buttons. Clicking these two buttons will open the same activity. No matter which button opens a new activity, when the new activity is closed, the system will call the onactivityresult (int requestcode, intent data) method of the previous activity. In onactivityresult() method, if you need to know which button opens the new activity and make corresponding business processing, you can do this:

4 function of result code

In an activity, you may use the startactivityforresult() method to open multiple different activities to handle different businesses. When these new activities are closed, the system will call the onactivityresult (int requestcode, intent data) method of the previous activity. In order to know which new activity the returned data comes from, you can do this in the onactivityresult() method (resultactivity and newactivity are the new activities to be opened):

5 demo source code:

TestResultActivity. java

TestResultActivity1

TestResultActivity2

The above is a detailed explanation of the activity instance introduced by Xiaobian to you. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>