Detailed explanation of jump and value transfer of Android activity

Detailed explanation of jump and value transfer of Android activity

Activity jump and value transfer mainly connect multiple activities and transfer data through intent class.

Intent is a very important class for Android. Intention is "intention". What is intention? For example, if you want to jump from one activity to another, this is your intention. Intent class plays a great role in Android system. I won't talk about it in detail here. There will be an article later!

Activity jump, no result returned

This is the simplest activity jump method. Start another activity from one activity and directly start activity (New intent (current activity. This, next activity. Class)).

Activity jump, return data / result

If you need to return data or results, use startactivityforresult (intent, int requestcode). The value of requestcode is user-defined and is used to identify the target activity to jump.

All the jump target activity needs to do is return data / results. Setresult (int resultcode) only returns results without data, or setresult (int resultcode, intent data) returns both!

The processing function for receiving the returned data / result is onactivityresult (int requestcode, int resultcode, intent data). The requestcode here is the requestcode of startactivityforresult, the resultcode is the resultcode in setresult, and the returned data is in data.

MyAndroidAppActivity:

SendSMSActivity:

**Note that after setresult, you should call finish() to destroy the current activity, otherwise you cannot return to the original activity, you cannot execute the onactivityresult function of the original activity, and you see that the current activity does not respond.

RESULT_ OK and result_ Canceled is a constant in the system space. It does not need to be customized. It can be used directly.

In addition, during operation, it is found that after pressing the back key, the result can be returned_ Canceled. After reading the SDK doc, it turns out that it is really like that, and there is no data. This means that if you imagine returning result_ When canceled and data is returned, it is necessary to intercept the event processing of the back key and return the original to result_ The core logic of canceled is copied into event processing. Here is an example:

It should be noted here that after processing the back key event, return true means that the event will not be passed to other functions for processing. It can be understood that the current function is fully responsible for processing. Therefore, finish the current activity before return to maintain the original behavior. Of course, we can set it to hide other behaviors such as the current activity.

Activity transfer data

In the above code, we can see that the bundle is used to store data and putextras it into intent. Bundle uses name value to store data. Then, when transmitting data from the original activity a to the new activity B, how does activity B obtain the transmitted intent parameter? Recently, I wrote such an example:

In Activity A:

In Activity B:

Thank you for reading, hope to help you, thank you for your support to this site!

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