在super.onBackPressed()上的onSaveInstanceState之后无法执行此操作

时间:2015-09-03 18:00:49

标签: android onbackpressed

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

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()上消失?

2 个答案:

答案 0 :(得分:14)

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

您应该确保在onBackPressed()覆盖该活动即将完成。可以出于其他原因按下后退。

答案 1 :(得分:3)

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

这与onActivityResult类似,当您展示插页式广告时,您的活动会暂停,而onAdClosed()时,您的应用尚未恢复。

要解决此问题,您可以尝试在onAdClosed()中设置一个标记,如果标记为开启,则在onBackPressed()中调用onResume()