开发者

How to not write spaghetti-code/huge activities w/Android?

开发者 https://www.devze.com 2023-03-15 20:38 出处:网络
I\'m trying to avoid having gigantic activity classes that are hard to follow, but the platform seems to be making it hard. Doing almost anything requires a activity or context - these can be stored a

I'm trying to avoid having gigantic activity classes that are hard to follow, but the platform seems to be making it hard. Doing almost anything requires a activity or context - these can be stored away in a helper class, but unfortunately calls like startActivityForResult, for example, don't take a sperate listener but always call back on the same activity on an override with a supplied integer code - so this means that seperation of concerns is hard - the main activity needs to kn开发者_运维技巧ow about the helper class and dispactch to the helper class based on an request code - and of course, that request code needs to be unique, so that's a leaky abstraction as well. Why not specify a listener for every call thus avoiding the dispatching and need for unique request codes? Any other ways to slice this? thanks


In API level 11 and higher, you can use Fragments.


Specifying a listener would be difficult since it would require passing an object to other processes in the Android environment- remember that activities can be called from one application to another.

How is using request codes making your activities "huge" in a way that listeners wouldn't? Activities should make use of classes defined elsewhere to do more most of the labor, let your activities pertain primarily to UI tasks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号