Android – why not use the “newinstance” pattern in the activity?

This seems to be a good way to simplify how to set parameters for fragments, so I'm curious why it's not often used to create explicit intentions for starting activities?

resolvent:

There is nothing wrong with creating a static method to build intent to start an activity, or even a static method to start an activity. You can also use the intentbuilder mode, in which you can show the builder style API for generating intent for scenarios where simple methods are not feasible. For example, I used this method in my cwac-cam2 library

Google has not yet promoted this model, but if you generate services from the Android studio wizard IIRC, you will see such code. However, Google has not really tried to show all possible programming models

The biggest limitation I know is that there is no enforcement mechanism. Although you can provide these types of facilities, nothing can prevent developers from bypassing or ignoring them. Iow, the classic create an intent and go API is "public", and you can't make it private in some way. Therefore, your activities need to bear the worst case and have defensive programming as you think appropriate

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