Detailed examples of Android intent encapsulation

Detailed examples of Android intent encapsulation

What is intent:

Intent is to coordinate the communication and interaction between applications and components. Through intent, you can start activities, services and broadcasts. Third party components can be called across programs. For example, start the call interface, music playback, etc.

Using intent:

Chestnut: add a click button in an activity - > Click to start the second activity (two string type parameters are required)

Click the button code of the first activity:

The second activity accepts intent data codes:

This is a common use of intent, but it has great disadvantages. If you have more and more projects, if you miss one parameter in starting the second activity, it may cause the program to crash. So how to avoid this situation:

Another idea is code implementation:

Click the button code of the first activity:

Code of the second activity:

Through comparison, it is obvious that we encapsulate the parameter passing logic required by intent into the activity to be started. The advantage is to start me according to my rules. My rules are mine. Moreover, when starting main2activity in other places, you can directly call newintent() to pass in the corresponding parameters.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can 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
分享
二维码
< <上一篇
下一篇>>