I am learning android from two resources one from video and one from book +(android developer resources ). Both show how to have multiple views in a different ways. Video one used activity and intent book used fragments. What is the best practice fragment/activity?When to use Activity and when to use Fragment.
You will most likely hear things like
You can have an Activity without a Fragment but you cannot have a Fragment without Activity
You can have multiple Fragments under a single Activity
You can use Fragments to have multiple sections in a single screen
Fragments has its own lifecycle
So I'd give my personal thought and a little bit of scenario based on my own experience on how I would decide which to use based on a given scenario.
Consider a Registration Form where I'm required to create 3 screen features of which contains Basic Personal Info, Financial Information and Online Mailing Information, in this case I would use multiple Fragments under 1 hosting Activity named RegistrationActivity.
Now consider same application where I'm required to create a User Setting Screen it wouldn't make sense if I create another Fragment under that same Activity right?, so that would be a separate context and everything about it will go to another Activity named SettingsActivity
There are more technical things under the hood, but most of the time you will decide which to use based on the use-case you are dealing with.
There is also an up to date architecture namely Modular design where each closely related components are grouped inside a module library, majority of such project will fall under the feature category where most of the time if not all, have single Activity hosting multiple fragments. And going back to my scenario it would be like a Registration Module
Hope it make sense.
Have a look at another similar post
Why fragments, and when to use fragments instead of activities?
I'm a beginner in android development. The app I develop has 30 activities. I'm in the process of adding admob ads to my app. Can I add ads only to 10 of 30 activities. Is it ok to add ads to only 10 activities, is it correct. If not is there any shortcuts to add ads to all activities. Shortcut in the sense programmatically adding ads to one activity and extending it to all activities.
First thing is that there is no shortcut to add it to all activities.
For putting the ad into the activities you want, put the JAVA and XML code only in the activity you want to place the ad.
Gradle is common for the whole project.
My Personal Advice - Do more code and get the concepts more well before indulging in the Ads. They are a bit complex for beginners.
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
I am trying to develop an application which have a single activity. I have achieved all the functionality using multiple fragments. I am showing advertisement using Admob in OnCreate of the activity.
Question:
Should I shift Admob code to OnCreate of each fragment for more impressions?
No. This might increase impressions but it not increase your revenue (revenue it a function of clicks not impressions) and may well decrease it (you may start getting lower value impressions because your CTR will drop). It will also unnecessarily complicate your code.
I've got following problem.
I got two different Activities in my Android application. Now I wanted to add an AdView on each of these activities.
It worked for me adding one adView to "BANNER" and the other one to "SMART_BANNER".
But now, when I try to set both AdViews (the one activity and the other activity), to "SMART_BANNER", I noticed that one of these Banners won't show up.
This made me ask several questions:
How many adViews am i allowed to implement in one app?
How many adVies am i allowed to implement in one single activity?
Why doesn't it work when using two SMART_BANNER in one app? Why does it work when using one BANNER and one SMART_BANNER adView in one class?
I hope you can help me.
It is against Admob TOS for you to display 2 AdViews on the one Activity.
But it is totally acceptable to display more than one AdView within your app. But in reality you only want to display AdViews where the user spends most of their time.