Can not perform this action after onSaveInstanceState on super.onBackPressed()

孤人 提交于 2019-12-08 14:46:29

问题


I am displaying an interstitial ad when the user presses back to exit the application:

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
   }
}

This is the crash report I received from my users:

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)

I understand that it is triggered when the user closes the ad, but why does it die on super.onBackPressed()?


回答1:


You can't call onBackPressed() when your activity is paused. However, the behavior on a back press is to leave the activity. Just call finish() instead of onBackPressed().

You should make sure in your onBackPressed()'s override that the activity is going to finish. Back can be pressed for other reasons.




回答2:


You can see the stack trace showing that the error is caused by FragmentManagerImpl.popBackStackImmediate.

This is just similar to onActivityResult, seems when you show the interstitial ad, you activity is put into pause, and when onAdClosed(), your app is not yet resumed.

To solve that, you can try to set a flag in onAdClosed(), and call onBackPressed() in onResume() if the flag is on.



来源:https://stackoverflow.com/questions/32382507/can-not-perform-this-action-after-onsaveinstancestate-on-super-onbackpressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!