sotiris
sotiris

Reputation: 335

android application restarts after InterstitialAd

I have created a game in android and i have noticed to a friend's mobile that when you click an InterstitialAd that transfers you to the google play store and then you return to the application with the back button , the application is restarted where it should continue from where it was

I am having trouble testing this because i am not allowed to click my own ads and this behavior cannot be reproduced with the test ads

I am not sure which part of the code to post

@Override
protected void onResume() {
    mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
    super.onResume();
}

@Override
protected void onPause() {
    mgr.unregisterListener(this, accelerometer);
    super.onPause();
}

and the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx"
    android:versionCode="8"
    android:versionName="1.7" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.xxx.FullscreenActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="landscape"
            android:label="@string/app_name"
            android:keepScreenOn="true"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!-- AdMobActivity definition -->
 <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

    </application>

</manifest>

Upvotes: 1

Views: 558

Answers (2)

Peter Sokolov
Peter Sokolov

Reputation: 29

Today I've got the similar problems (my game is restarted after admob Interstitial Ad shows). But I got it not after Ads was clicked as in your case (I also can not click ads into my game as I'm developer too), but I got it after video ads tried to show in Admob InterstitialAd.

I connected device to PC, opened LogCat and start play the game until video ads appeared again and I catched the reason of restarting :

06-09 19:04:07.445: W/System.err(29032): java.lang.SecurityException: Neither user 10124 nor current process has android.permission.WAKE_LOCK.
06-09 19:04:07.445: W/System.err(29032):    at android.os.Parcel.readException(Parcel.java:1465)
06-09 19:04:07.445: W/System.err(29032):    at android.os.Parcel.readException(Parcel.java:1419)
06-09 19:04:07.445: W/System.err(29032):    at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:302)
06-09 19:04:07.445: W/System.err(29032):    at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:719)
06-09 19:04:07.445: W/System.err(29032):    at android.os.PowerManager$WakeLock.acquire(PowerManager.java:688)
06-09 19:04:07.455: W/System.err(29032):    at android.media.MediaPlayer.stayAwake(MediaPlayer.java:1153)
06-09 19:04:07.455: W/System.err(29032):    at android.media.MediaPlayer.start(MediaPlayer.java:1063)
06-09 19:04:07.455: W/System.err(29032):    at com.android.org.chromium.media.MediaPlayerBridge.start(MediaPlayerBridge.java:98)
06-09 19:04:07.455: W/System.err(29032):    at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
06-09 19:04:07.455: W/System.err(29032):    at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
06-09 19:04:07.455: W/System.err(29032):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-09 19:04:07.455: W/System.err(29032):    at android.os.Looper.loop(Looper.java:136)
06-09 19:04:07.455: W/System.err(29032):    at android.app.ActivityThread.main(ActivityThread.java:5038)
06-09 19:04:07.455: W/System.err(29032):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 19:04:07.465: W/System.err(29032):    at java.lang.reflect.Method.invoke(Method.java:515)
06-09 19:04:07.465: W/System.err(29032):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-09 19:04:07.465: W/System.err(29032):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
06-09 19:04:07.465: W/System.err(29032):    at dalvik.system.NativeStart.main(Native Method)

I.e. it required WAKE_LOCK permission. After I added it :

<uses-permission android:name="android.permission.WAKE_LOCK" />

I play the game again, until video ads was not shown in Interstitial Ads and now it works fine!!!

So, I think, it can also solve your problem too.

Upvotes: 0

William
William

Reputation: 20196

You cannot stop Android from destroying your Activity once it is no longer in the foreground. It is part of the Android framework.

What you need to do is to use onSaveInstanceState and onRestoreInstanceState to stash your Activity's state when it is destroyed so that your application can continue after being interrupted by another app (like Google play or a the phone dialer).

Upvotes: 1

Related Questions