18

当用户按下退出应用程序时,我正在显示一个插页式广告:

mInterstitialAd.setAdListener(new AdListener() {
   @Override
   public void onAdClosed() {
        onBackPressed(); //line 98
   }
});

requestNewInterstitial();

private void requestNewInterstitial() {
   AdRequest adRequest = new AdRequest.Builder().build();
   mInterstitialAd.loadAd(adRequest);
}

@Override
public void onBackPressed() {
   if (mInterstitialAd.isLoaded()) {
      mInterstitialAd.show();
   } else {
      super.onBackPressed(); //line 410
   }
}

这是我从用户那里收到的崩溃报告:

java.lang.RuntimeException: Unable to pause activity {com.myapp/com.google.android.gms.ads.AdActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3088)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3043)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3021)
at android.app.ActivityThread.access$1000(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:493)
at android.app.Activity.onBackPressed(Activity.java:2215)
at com.myapp.MainActivity.onBackPressed(MainActivity.java:410)
at com.myapp.MainActivity$1.onAdClosed(MainActivity.java:98)
at com.google.android.gms.ads.internal.client.zzc.onAdClosed(Unknown Source)
at com.google.android.gms.ads.internal.client.zzm$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.ads.internal.client.l.a(SourceFile:109)
at com.google.android.gms.ads.internal.a.n(SourceFile:645)
at com.google.android.gms.ads.internal.b.t(SourceFile:351)
at com.google.android.gms.ads.internal.t.t(SourceFile:141)
at com.google.android.gms.ads.internal.overlay.ab.n(SourceFile:672)
at com.google.android.gms.ads.internal.overlay.ab.i(SourceFile:408)
at com.google.android.gms.ads.internal.overlay.a.d.onTransact(SourceFile:86)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.internal.zzdj$zza$zza.onPause(Unknown Source)
at com.google.android.gms.ads.AdActivity.onPause(Unknown Source)
at android.app.Activity.performPause(Activity.java:5335)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3074)

我知道它是在用户关闭广告时触发的,但为什么它会死掉super.onBackPressed()

4

2 回答 2

16

onBackPressed()当您的活动暂停时,您无法拨打电话。但是,背压的行为是离开活动。只需调用finish()而不是onBackPressed().

您应该确保在您onBackPressed()的覆盖中活动将完成。后退可能因其他原因而被按下。

于 2015-09-04T05:39:49.563 回答
5

您可以看到堆栈跟踪显示错误是由 FragmentManagerImpl.popBackStackImmediate 引起的。

这类似于onActivityResult,似乎当您显示插页式广告时,您的活动被暂停,而当onAdClosed()您的应用程序尚未恢复时。

为了解决这个问题,您可以尝试在 中设置一个标志,如果标志打开则onAdClosed()调用。onBackPressed()onResume()

于 2015-09-04T05:32:37.140 回答