user4903
user4903

Reputation:

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, it 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?

Upvotes: 0

Views: 1926

Answers (3)

BonanzaDriver
BonanzaDriver

Reputation: 6452

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.

Upvotes: 0

shihpeng
shihpeng

Reputation: 5381

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.

Upvotes: 1

user432209
user432209

Reputation: 20177

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.

Upvotes: 1

Related Questions