如何分享在各种活动视图视图

2023-09-08 09:13:25 作者:余厌

有没有办法分享在各种活动视图相同的对象?例如对myApp有4个活动,每个活动显示在屏幕顶部的标志。现在每个活动将启动相同标志的4份。那么,有没有办法来解决这个问题?结果如果四分之三的份额相同的标志?

Is there any way to share the same object of the View across various activities? For example myApp has 4 activities, and every activity shows a Logo at the top of the screen. Now each activity will initiate 4 copies of the same Logo. So is there any way to get around this? And if 3 out of 4 share the same logo?

推荐答案

我不能说这是完全不可能为你做到这一点。我几乎可以肯定地说是,你不应该尝试它,或者指望什么好,如果你管理它发生。不打框架,让它为你工作。如果复制是个大问题,你创建一个抽象类,你的活动继承。

I can't say that it is completely impossible for you to do that. What I can say with almost certainty is that you should not attempt it or expect anything good to happen if you do manage it. Don't fight the framework, let it work for you. If the duplication is that much of an issue you create an abstract class that your Activities inherit from.

此外,由于景观的维持他们创建上下文的参考。如果您没有管理从一个活动传递一个视图到另一个你会创建一个内存泄漏。由于视图将举行通过其创建的上下文的引用老活动。

Also, since View's maintain a reference to the Context they were created in. If you did manage to pass a View from one Activity to another you would be creating a memory leak. Since the View would hold a reference to the old Activity via the Context it was created in.