Android: Reusing same View object in different Activities (the case is about ad banners)

后端 未结 5 1539

I want to reuse the same view object (not the view class, or the xml file, I mean the object in the memory) in different Activities.

I almost had this done. The thin

5条回答
  •  -上瘾入骨i
    2021-01-08 01:44

    This isn't really possible in the form you are asking. The views in an activity must be inflated at load time, copying/referencing them in-memory is unlikely to work how you want it to.

    Instead, you should look to building the view in each activity you need it, and transferring just the data you need to populate it instead.

    If you are trying to improve the performance of your app, I would recommend looking for ways to simplify your view, rather than violating the activity lifecycle.

    Update:

    Since you've revealed the purpose behind this question (intercepting served ads from a third-party library), I suggest you first contact the company and check the terms of use. If they permit the use of their service while bypassing the View code, then they might be able to provide you with a lower-level API for displaying the ads as you see fit.

    If such use is not permitted, consider the fact that they might block your account (and withhold payment) for mis-use.

    If you still want to go ahead: DON'T hack away at the Android UI patterns to make this work. Extract the ad images from the third-party library server-side (i.e. building a simple hosted Java webapp with cache store and a REST API) and serve the ads to your Android app from this "man-in-the middle" service. I certainly do not endorse this method, however.

    I accept you are seeking some penultimate technical solution for your approach, but I genuinely think it is the approach itself that is the problem here. If I was in your position, I would start to look for other Ad serving solutions that better fit my requirements, as well as contacting the third-party to see if I can pay for more customised integration. Anything involving transferring inflated Views between activities is doomed to constant maintenance problems, if it works at all.

提交回复
热议问题