开发者

includeed layouts - update in multiple activities

开发者 https://www.devze.com 2023-03-07 18:52 出处:网络
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 acti

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 开发者_JAVA百科to make it update the elements in a new activity?


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....


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).

0

精彩评论

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