包含的布局 - 在多个活动中更新
我有一个具有多种活动和多种布局的应用程序。然而,一份布局包含在多项活动中。我还有一个更新此布局的线程。但是,当我切换活动时它不起作用。由于包含布局,因此元素具有相同的 ID,难道它不应该工作吗?或者我真的需要为布局中的每个元素获取一个对象并将其输入到我的线程中以便使其更新新活动中的元素吗?
I have an app with multiple activities and multiple layouts. However, one piece of layout is included on several activities. I also have a thread which updates this layout. However, when i switch activity it doesn't work. Since the layout is included the elements have the same ID's, shouldn't it just work? Or do I really need to fetch an object for each element in the layout and feed it into my thread in order to make it update the elements in a new activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该为每个活动/视图运行更新代码,尽管包含的 XML 是相同的,但每个都是不同的实例。
我的建议是在重新启动时验证每个活动是否需要进行任何修改,一个简单的方法是每个活动扩展一个具有此代码的 BaseActivity。
You should run the update code for each Activity/View, although the XML included is the same, each is a different instance.
My suggestion is on Restart verify is there is any modification to do in each activity, a simple way is to each Activity extend a BaseActivity that has this code.
我在我的应用程序中包含了广告布局,但在使用它的每个活动中,都需要重新加载广告。
如果我从使用相同包含布局的活动中调用一个活动,那么当我返回到上一个活动时,它仍然存在。
我想这就是你所看到的......
I include a layout for adverts in my app, but on each activity that uses it, the adverts need to be reloaded.
If I call an activity from one that is using the same included layout when I go back to the previous activity it's still there.
I guess this is what you are seeing....
因此,您还可以将该数据保存在sharedPreferences中(如果它是小数据和原始对象或可解析对象)。
您还可以扩展 Application 类并将数据存储在那里,并更新 onResume() 方法内的每个活动。我认为这是处理这个问题的最佳方法。这很简单。
询问谷歌有关扩展应用程序类的问题,他将提供大量关于如何做到这一点的结果。这是在活动之间传递数据和/或保留对将在整个应用程序中使用的单个对象的引用的简单方法。当您不再需要它时,请小心清除它,因为它将一直存在,直到应用程序完成()(它随应用程序扩展一起存在于整个应用程序生命周期中)。
So you can also save that data inside sharedPreferences (if it is little data and primitive objets or parceable objects).
Also you can extend the Application class and store the data there and update every activity inside the onResume() method. that i believe is the best way to handle this. and this is quite simple to do.
Ask google about extending the application class and he will provide tons of results on how to do it. its an easy way to pass data between activities and/or keep a reference to a single object which you will use throughout the app. Just be carefull to clear it when you wont need it anymore because it will stay in existance untill the application is finished() (which comes with the application extension living thru the whole application lifetime).