开发者

Hooking up a new android component to main activity

开发者 https://www.devze.com 2023-02-11 03:41 出处:网络
So i wrote a scrolling image component which uses relative layout\'s , linear layouts , AsyncTasks and canvas drawing. While developing I tested the application by writing everything inside a dummy a

So i wrote a scrolling image component which uses relative layout's , linear layouts , AsyncTasks and canvas drawing. While developing I tested the application by writing everything inside a dummy activity. Since my component uses threads to communicate with the server, the t开发者_运维知识库hread creation and destruction are related to the activity life cycle.

Now, i want to embed this component inside the real main activity of application. So my question is there a way to make my component aware of activity life cycle events. The approach I have used now is to create a matching life cycle method such as (onMyComponentCreate()) and call it inside onCreate. Is there another way to do that?


You can subclass Activity and use a publisher/subscriber pattern to notify subscribers of Activity lifecycle events.

Create an interface that has the same methods (onCreate, onPause, etc) and have your control implement this interface. Add a List of these interface types to your Activity sub-class and expose public methods for attaching and detaching subscribers (don't forget to clean the references up in onDestroy). Then, override all of the life cycle methods and after calling the life cycle methods on the super class (super.onCreate, etc) loop through all subscribers and call the same method on them.

0

精彩评论

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