How to avoid unnecessary reloads of Google Ads on my Android App - android

I made an Andriod App, it's a typing game, that have several levels. The thing is that I'm showing ads in the bottom of the screen every time that the player fail (because if you type wrong, you fail)
So the thing is that every time that the player fail the activity reloads to start again. So with that, the ad is reloading and doing another request.
I try to search if it's any way to avoid this but I was not able.
So what I want to do, is to make and Adrequest just the first time that the game is loaded, and then just put it visible or invisible.
By the way if you want to take a look at the app is in the google play:
Doctor Key
Thank you!

I would redesign your Activity so that it doesn't have to recreate itself every time you start again. Maybe add reset() and setLevel() methods that reinitializes everything and set the state for whatever level you're doing.
Alternatively you could look into using Fragments. Then you simply replace the level fragment, while leaving the ad section in a separate Fragment that would not be affected.

Related

How to avoid re-loading of Admob ads upon configuration change and starting an Activity?

Using the banners sample here, I noticed that if I change the orientation, the ad is gone, and then reloads itself (takes time), so there is a time that nothing is shown, each time I change orientation.
I know it's possible to set configChanges for the activity via the manifest (and it actually works), but this is not a recommended thing to do in general. It has multiple flaws:
In my case, for example, changing the orientation let the toolbar stay on the same height, which is wrong because it should be different on landscape than on portrait.
Going to another Activity the Admob view will still get re-loaded, even though it was loaded before.
When going from portrait to landscape, and vice versa, the ad doesn't get to fit well on the new orientation.
What else can be done to avoid re-loading of ads, on each configuration change?
I want Admob to load ads once (and the rest in the background, for new ones), and then show them, and avoid re-loading when I change orientation. Just show what's best for the current configuration (orientation for example), each time I reach it.
Same goes for going to a new Activity that has an Admob ad. If it loaded before, no need to re-load.
EDIT: I've put a bounty on this, because Google/Firebase support told me via email it's actually possible to cache the ads:
The Ads SDK won't cache ads for you, however you can implement an
'AdManager' style class, most likely a singleton or an instance
attached to your application instance, which can manage caching for
you.
As you recognize it's an Android pattern to destroy and recreate
activities on orientation, instance state is usually saved and
restored via a bundle, but this is not applicable to views.
If you wish to optimize the reload time of the banner view in this
scenario, I would recommend making your ad request via an AdManager
class, and the AdManager can then apply some logic to cache ads such
as your banner view for a certain amount of time, and return the
previous banner if it is still valid for the new request (which you
would make on orientation change).
And :
This idea of caching ads is not implemented by the SDK, as it is
difficult to determine the best caching logic to apply in all cases,
thus it is left to the developer to implement caching that suits their
use case, if desired.
Possibly having a banner ad decoupled from the 'AdView' class would
make for more appropriate cacheable objects, however this is not
currently the case.
Therefore as it is, yes we suggest that in order to prefetch / cache a
banner ad, you need to store the 'AdView'. Instead of using an
AdManager however, another supported and indeed recommended approach
would be to use a ViewModel, see
https://developer.android.com/guide/topics/resources/runtime-changes#RetainingAnObject
and
https://developer.android.com/topic/libraries/architecture/viewmodel.
These are designed for exactly this kind of use case.
If it is indeed possible, I'd like to know exactly how to do it. Please, whoever succeeds in doing it, show in code what can be done.
I don't see clear way how to cache ordinary ads. Nor singleton, nor viewmodel won't help, we can't cache/retain the view itself and there is no public ad object to retain.
However there is AdLoader using which one can load native ads, and retain loaded UnifiedNativeAd object. Then it can be used to set ad in view.
Other ad sdks provide way to prefetch ads though.
P.S. dunno why stack brought this in search results for "android viewmodel" query
Seems that having a native ad is the best option instead of configChanges . The pure data can be saved on the ViewModel of the Fragment/Activity, for example, so that you could re-use it again.
Sadly though, it's a solution you have to implement yourself, including not just how it looks, but also the loading procedure itself, including when to reload it yourself.
There is a lot to read and to implement based on a person's need, so best thing to start is from :
https://developers.google.com/admob/android/native/start
Not sure though if in terms of income, it's about the same as banner ads.
i think that using smart banner would be a good option, because they work for any orientation
To use Smart Banners, specify the constant SMART_BANNER for the ad size:
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
also check this:
https://firebase.google.com/docs/admob/android/banner#smart_banners

How come if you click an ad in my game it causes the game to restart?

I just started to experiment with ads for the first time so I am a complete beginner at this stuff. I have the ads running at the right screens which is good but if you click an ad and it takes you out of the game it causes the game to restart. I made my game in libgdx and I use the pause function and what not but it obviously doesn't call that and in fact just causes a full restart which would make a lot of users angry. Anyone else having this issue or I am doing something wrong?
I am using admob by the way.
EDIT
I also just realized that it happens on my nexus 5 and not my nexus 7.
I think it happened to me some while ago with a game that tried to implement fb sign in functionality. The second it got back from the sign it, it restarted.
What might be happening in your case is that when you click on the add, android switches from your LibGDX activity to the admob activity. Once you come back, the onCreate method gets called again, therefore restarting your app from scratch. (A simple way to verify this is just to add a Log call everytime the app on create method is entered, and check the behaviour of your app)
If that is the case, a possible workaround for it is checking that if your game has been already initialized it should not instantiate it again.
The restart means that your app has crashed. That's standard Android behaviour.
I suspect you have wired something up incorrectly. Look at you log. There's a good chance something hasn't been initialised that should have.

0 second load time for Admob Ads in Android

The question is in the context of Admob in Android.
In every activity, the admob ads is taking couple of seconds to load. I want that the Admob Ads should load instantly with the activity.
I have tried couple of solutions but nothing seems to work :(
So i thought of Frameset concept in HTML, if we click on the link, in a particular frame then only that part will refresh and the other part remains static.
So i was hoping for similar solutions in Android, where the admob ads remain static and the rest part of my activity changes accordingly.
Can anyone guide me, on how to implement similar functionality in Android ?
Also if you have any other alternative to the Admob Ads 0 seconds load time, kindly share it.
Admob takes about a couple of seconds because instruction to load admob is called only when activity started. That's why every admob is also reloaded as long as you reload your activity. In addition, this is often a remote call and could take a time.
Solution :
Create a main activity (which contain admob loading) which handle your set of fragments. I mean instead of using activities, you should use fragments handled by only one activity.
So, you can navigate between all of your views using fragment manager without making any change to admob loading (because it owns to main activity)

Android app display on top of another

I'm trying to build an application that would run at the same time as another one, on top of it (hiding only partially the original app), that would display useful information for the user of the main app.
But it seems that 2 activities cannot run at the same time, overlap, etc...
For argument sake, lets say the app would be displaying date & time in a corner of the screen, while playing a specific game.
Anyone got any doc or sample code on how to achieve such a result ?
I'm also interested in how to know which app is currently running in order to decide in my app-on-top to be visible or invisible.
Any help appreciated :)
Thx
Looks like you are in need of Fragments: Fragments
The Android system is designed to be user friendly, and two activities at same time is not, so only one activity is on foreground at once. if you need the other to be running in background make it a Service, if you want to show some data and get back first one use a Dialog, finally if you want both you can either put them in one activity or use Fragments as #Tooroop suggsets.
Its propably too late ... but for others with the same problem:
check out how-to-draw-on-top-of-other-applications
and maybe also this if your app need to be on top on fullscreen apps

Android version dilemma

On calling back my first activity ,it should call onResume.It is calling the same in android version 2.2.But when I checked in for android 4.1 it is calling onCreate method that calls splashscreen and hence it looks like the app is restarting.How could I make sure that onResume is called for every version of android?
Thanks
As Henry said, you can't guarantee that onCreate won't get called again - you're not in charge of that lifecycle. If the system decides to get rid of your activity while it isn't in the foreground, then when you return to it it will be recreated. If you want to make sure that you don't show the splash screen again you will need to save state to say that it has been shown (e.g. using onSaveInstanceState). As an aside, splash screens generally aren't a great idea on android, partly for this reason. It's better to view your app as a loose collection of activities that can be entered and reentered somewhat randomly, as the android system basically does. Where you see splash screens used on android, it's common to see them slightly misbehave.
If you want to keep track of your application lifecycle, to which a splash screen might correspond, then you can subclass the Application object and put a flag there. However, android may leave your app running for weeks, so the user won't necessarily see that splash screen very often.
Keep in mind that the true purpose of a splash screen is supposed to be to show the user something nice while a long loading process is happening, and not to put your branding in their face. If you use the approach with onSaveInstanceState (and often onRetainNonConfigurationInstance too) then the cases where you show the splash screen would indeed be those where you need to redo that loading process, so that would be correct. However, it's generally better to rethink the design and bring up a minimal UI quickly, then show that some data is loading.
Finally, here's someone who goes into great depth on this subject: http://android.cyrilmottier.com/?p=632
If the system has removed your activity while it was unused, it will receive an onCreate thats normal behavior.
You could make the splash screen part of a separate activity to avoid the problem.

Categories

Resources