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

后端 未结 5 1535

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条回答
  • 2021-01-08 01:40

    Why don't you use Fragments?

    http://developer.android.com/guide/topics/fundamentals/fragments.html

    I think your use case is perfect for this.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-08 01:48

    I'm in the same case as Danail. The thing is not about to hack de Ad provider, but that if you want to show a banner through different activities it's a best practice in advertising not to reload it every time you change the activity, because you do more impressions so you CTR (Click Through Ratio) will decrease. You'd rather reload the banner at the time rate you fix, independently of activity changes.

    I think the right way to do that would be, as NeTeInStEiN says, using fragments. You could have a unique activity composed by different fragments. In one of the fragments, for example at the bottom, you'd have the banner and you'd load it actually once. Then on the bigger area on the top (let's say we are on a handset) you'd place different fragments, one at a time, that would correspond to your existing activities. When you'd normally "change" the activity, now you'd just change the "main" fragment, but the banner fragment will stay there unchanged.

    The main and BIG problem about this approach is that you need to design you app this way from the beginning, because changing the app model from several activities to one activity with several fragments is quite a big code change... :.(

    So I understand that, for implementation costs, one could try to "carry" the view from one activity to another. But as I saw in other responses, it's really not recommended and a troublesome way...

    0 讨论(0)
  • 2021-01-08 02:04

    I'm keeping in mind that you can afford to leak 1 activity, as this is the only solution I know: Declare a static view, say myAdView in your 1st activity (in which you are requesting ad). Now you can ad and remove this myAdView in every activity transation. Ofcource you will have to maintain seperate LinearLayouts for ur ads in seperate activities, where we will add/remove the myAdView eg. Suppose you are going from activity A to B, then in A's onPause remove myAdView:

    private LinearLayout layoutAd;
    layoutAd = (LinearLayout) findViewById(R.id.layout_ad); // from A's xml
    protected void onPause() {
        super.onPause();
        layoutAd.removeView(FirstActivity.adBannerView);
    }
    

    and in B's onResume add the same (FirstActivity's) myAdView:

    private LinearLayout layoutAd;
    layoutAd = (LinearLayout) findViewById(R.id.layout_ad);  // from B's xml
    protected void onResume() {
          super.onResume();
          layoutAd.addView(FirstActivity.adBannerView);
    }
    

    Hope this solves your problem to some extent.

    0 讨论(0)
  • 2021-01-08 02:06

    If you want a variable to be used within multiple activities then best practice for it is to put them in a separate class (can be named as Constants or MyVars) as static variable and use them in any activity you want as like Constants.SavedVar or MyVars.SavedVar as below is code example.

    public class MyStaticVars {
    
    public static Context myContext;
    }
    
    // First Activity where you want to save a specific context
    
    MyStaticVars.myContext = ContextToBeSaved;
    
    // Any Other Activity where you want to reuse that context
    
    priviousContext = MyStaticVars.myContext;
    
    0 讨论(0)
提交回复
热议问题