I noticed that my app was receiving few clicks on the InterstitialAd, and I saw that the Ad could be instantly closed by pressing the Back button. Usually the ad takes a few seconds to appear, so the user can close it even before seeing it (when only a black screen appears).
I don't think that it's fair, is almost useless to show it. Is there anyway to prevent this from happening?
I did this, but nothing happened...
// When creating the InterstitialAd, I set the following listener
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
isShowingInterstitialAd = true;
mInterstitial.show();
}
#Override
public void onAdClosed() {
isShowingInterstitialAd = false;
mAdView.setVisibility(View.VISIBLE);
mAdViewClose.setVisibility(View.VISIBLE);
}
});
// On the Activity's class
#Override
public void onBackPressed() {
if (isShowingInterstitialAd) {
return;
}
// ...
}
My understanding is that AdMob draws its own Activity on top of yours, so it has its own implementation of onBackPressed(), which means that you don't have control of anything that happens once you call mInterstitial.show(); until AdMob gives control back to your activity.
Don't sweat the little things like this.
You are much better off spending energy making your app better.
Some people will never click on ads. But if you provide an inapp purchase that removes the ads they may well buy that. That is a better return on investment for your dev effort.
IMHO
Depending on your OS, there are a few ways to override that back button. Search for back button override to find them.
Related
I've followed the example pattern for handling the android back button in the react-native docs and it works well. I can use the hardware back button to pop my navigation stack.
At the point that there's only 1 view in the stack though I don't pop it (just like the example), and I return false from my hardwareBackPress event listener. At this point it I see the componentWillUnmount method being called in my final view, at which point my app shuts down.
If I return true then nothing happens at all obviously.
What I want to happen is that the app merely gets "backgrounded" instead of exiting completely.
Answered my own question. The trick is to override the default back button behaviour in the MainActiviy:
public class MainActivity extends ReactActivity {
#Override
protected String getMainComponentName() {
return "foo";
}
#Override
public void invokeDefaultOnBackPressed() {
// do not call super. invokeDefaultOnBackPressed() as it will close the app. Instead lets just put it in the background.
moveTaskToBack(true);
}
}
Though I may be very late in giving the answer it may help other facing the issue.
Recently I came across the same requirement where I have to move the app to the background. I tried the solution provided by #pomo. Though it worked I faced problems. Sometimes on multiple clicking of the back button, the app misbehaves in android though it worked perfectly fine in iOS.
And then I came across the following issues in GitHub where it mentions the reason for the misbehaviour.
The following solution works perfectly fine now.
// android/app/.../MainActivity.java
#Override
public void invokeDefaultOnBackPressed() {
moveTaskToBack(true);
}
<!-- AndroidManifest.xml -->
<activity
...
android:launchMode="singleTop">
Link from where I get the solution
I hope I'm able to help guys with the same requirement.
I implemented admob interstitials into my app and everything is working fine.
Admobs interstitials are full screen and this is fine, the problem is that somehow when the admob interstitial is shown in full screen, my app (in the background) also jumps into full screen.
Now when someone closes the ad, the app jumps from full screen back to being normal again and this looks like a bug to me, because it causes a small flickering effect.
Did anyone else notice this effect, or I am alone in this?
Is there some way to fix this? Can i force my app to stay that way and not jump to full screen? Or can I force the status bar on interstitials ads?
Thank in advance!
Edit, my sourcecode is very similiar to this (standard approach taken from google itself):
public class BannerExample extends Activity {
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(MY_AD_UNIT_ID);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
}
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
update:
i integrated mopub which also offers full screen interstitials and interestingly enough this does not happen with mopub even though integrating it in the app is similar to admob. so i bet this is some odd behaviour of admob and not some bug in my app
It really depend on what is going on during Activity.onResume, when it call resume it try to update the view. It may look like flickering, but it just updating the view. Check your class and print some debug log to see what is it doing when you click the close button.
AdMob SDK don't control your class, so it only dismiss the ad view. What you do in your class cause the flickering. Without seeing the code, I cannot say for sure, maybe some code would be great.
Check my reply here:
Phonegap screen flickers with AdMob animation
Perhaps is a similar issue. Not every developer has the luxury of switching to a different provider (ie. if you work with a client)
i know that may duplicate some threads but i can#t figure out what i wrong. I have slider dreawer where are fragments and in fragments there are webview. Everything is working fine at least one thing, namely when i press back button it closes the app. I have tried some other possible solutions but anything is not working. I even don't get any errors. I even tried this easy solution but without any progress
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
My Main activity:
And this is one of my fragments:
By default back button shall close the app, if you are at the main/landing activity(there are other ways as well). If you want to override backbutton behavior, you should be overriding onBackPressed(), which you are doing right, but you should avoid calling super.onBackPressed() (since this gives you the default behavious, i.e. closing the activity OR finish(), with this you're closing the activity yourself. which is what you want to avoid.
Hope it helps.
#Override
public void onBackPressed() {
if (mSlidingDrawer.isOpened()) {
mSlidingDrawer.close()
} else {
Toast.makeText(MyTestApplication.getAppContext(), "Closing application", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
}
Does anyone know if any of the Android Advertising SDKs work with the new DreamService functionality? I tried using AdMob and first saw that the Interstitial class constructor explicitly requires an Activity. I saw the the AdView has a constructor that just needs a context so I tried that, but got a runtime exception telling me the problem was that I'm trying to inflate an AdView using a Context other than Activity. I looked into trying the Amazon Mobile Ads API, but it appears identical to the AdMob one.
I tried to get creative and start another Activity from my DreamService that creates an Interstitial ad, but it was created behind the DreamService UI (kinda makes sense since the Daydream overlays everything). Does anyone know of any solution to using Ads in a Daydream?
I came up with something that solves this issue, though I still don't really like the solution. Would welcome a more elegant approach if anyone knows of one.
What I did was use the mMedia SDK instead of AdMob. Their Interstitial and AdView classes can both take a Context rather than an Activity in the constructor. The Interstitial still didn't work out for me since it opens behind the Dream overlay. So what I ended up doing was adding an AdView to my Dream's XML layout, then setting its visibility to View.GONE until I wanted to display it. When it's time to display the ad I set it to View.VISIBLE.
The other issue I encountered was that after clicking the AdView it launches the browser with the ad's URL, which of course opens behind the Dream, defeating the purpose of showing an ad. So I ended up setting the Dream to be interactive, caught the onTouchEvent, then if the Ad is VISIBLE when the click happens call the Ad's callOnClick method. I also had to set the Ad's RequestListener to my Dream Service and implement the MMAdOverlayLaunched method, which is called when the Ad launches the browser. In this method I just called finish() to stop the Dream and let the browser display the Ad.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
// Exit dream upon user touch
setInteractive(true);
// Hide system UI
setFullscreen(true);
// Set the dream layout
setContentView(R.layout.dream_layout);
//Initialize Ads
this.initAdvertising();
}
private void initAdvertising(){
MMSDK.initialize(this);
mDreamAd = (MMAdView) findViewById(R.id.adView);
//Separate thread will handle showing the ad
mDreamAd.setVisibility(View.GONE);
mAdRequest = new MMRequest();
//TODO add metadata to Request
mDreamAd.setMMRequest(mAdRequest);
mDreamAd.setListener(this);
mDreamAd.getAd();
}
#Override
public boolean dispatchTouchEvent(MotionEvent event){
super.dispatchTouchEvent(event);
if(mDreamAd != null && mDreamAd.isShown()){
mDreamAd.callOnClick();
}
return true;
}
#Override
public void MMAdOverlayLaunched(MMAd ad) {
//Finish so we can display the ad the user has clicked
if(ad.equals(this.mDreamAd))
this.finish();
}
I have overwritten the functions of the back and the homebutton, to prevent users to use the phone as a phone. We´re giving phones to clients (students) and we don´t want them to abuse the phones in a certain mode.
The thing is, the HOME button is disabled, but when I first open the option menu and then don´t select an option, but press the HOME button, the HOME still works as normal.
Is there anyway to overwrite this and use a boolean, sometimes yes, sometimes no..
What I got now is this:
#Override
public void onBackPressed() {
if (buttonslocked) {
//backbutton blocked!
} else {
super.onBackPressed();
}
}
#Override
public void onAttachedToWindow()
{ //HOMEBUTTON
if(buttonslocked)
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
else
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
}
}
You cannot override the home button functionality.
Just as a clarification, although you may see some hacks that might provide what you are looking for.. This is unsupported and discouraged!