开发者

How can I allow multiple activities to share the same inflated "included" view?

I see this question here, and it makes me wonder if what I'm asking isn't really possible:

How to share a view across various activities

Basically, I have a common footer view that I'm inflating (including) in all of my views. However, i开发者_如何学Ct uses the same repetitive code to do that. My thought was to create a parent activity class to do this, but it doesn't seem correct to have one activity render the view of another. So should I just create a utility class of some sort, or is there a better way?


You can include other layout XML files directly in another layout file. So whenever you set content to a layout file, along comes your footer for the ride.

If your footer needs code to drive it, just create a custom class for it along with the layout file. Then perhaps during instantiation you can drive the code that needs to execute.

This is a blog of how to do it.


include is very useful while reusing View components.

But remember, if any problem occurs while using include tag, wrap the included view by an arbitrary layout.


If I understand your question, another way of having multiple Activities use the same View instance is by doing something like creating your own Application class (it's seriously easy).

MyApplication extends Application ...

@Override public void onCreate(), onConfigurationChanged(), onLowMemory(), onTerminate(), getIstance().

As there is only a single intance of "Application" that is statically available it makes it a good place to store and share various objects that need to be passed around.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜