Detailed usage of startactivityforresult() in Android Foundation

preface

A very basic operation in Android development is to open an activity. Another necessary operation is to open an activity, operate in the opened activity and obtain the returned results.

Two activities

In order to demonstrate this operation, we need two activities: mainactivity and otheractivity. In mainactivity, start otheractivity through intent and obtain the results returned after otheractivity. The example is only for demonstration. For convenience, I directly define the control in the java file and do not write the layout file.

MainActivity.java

OtherActivity.java

The execution logic of the whole code is as follows:

1. The code of the two activities is like this. After the onclick () event is triggered for the button in the mainactivity, the EditText content in the mainactivity is sent to otheractivity.

2. After otheractivity opens and initializes the control, set the content of EditText to the content sent by mainactivity.

3. After the onclick () event is triggered for the button in otheractivity, the EditText content in otheractivity is returned to mainactivity.

4. When returning to mainactivity, the content of ononactivityresult() method will be executed. If the requestcode and resultcode are correct, change the content of EditText in mainactivity to the result returned by otheractivity. (the requestcode and resultcode are determined here, so when otheractivity returns through the back key, the content of mainactivity will not change)

As a result, when these two activities are opened in this way, the contents of EditText in them are synchronized. If one activity changes, it will change in another activity.

summary

Well, the above is all about startactivityforresult() in Android. I hope the content of this article can bring some help to Android developers. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>