1

在我的应用程序中,我得到java.lang.IllegalStateException:onSaveInstanceState后无法执行此操作。在Activity的onBackPress()之后。java.lang.IllegalStateException:无法在onSaveInstanceState之后执行此操作On Activity Backpress(未使用任何片段)

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(MyApplication:1533) 
     at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(MyApplication:603) 
     at android.support.v4.app.FragmentActivity.onBackPressed(MyApplication:179) 
     at com.app.mobile.views.NewsActivity.onBackPressed(MyApplication:301) 
     at com.app.mobile.views.NewsActivity.onOptionsItemSelected(MyApplication:293) 
     at android.app.Activity.onMenuItemSelected(Activity.java:2625) 
     at android.support.v4.app.FragmentActivity.onMenuItemSelected(MyApplication:406) 
     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(MyApplication:195) 
     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(MyApplication:103) 
     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(MyApplication:103) 
     at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(MyApplication:183) 
     at android.view.View.performClick(View.java:4446) 
     at android.view.View$PerformClick.run(View.java:18437) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5447) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

在我的活动中,我没有使用任何片段。我寻找解决方案,但只发现碎片。

在我的代码中。

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
     case android.R.id.home: 
     onBackPressed(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 

@Override 
    public void onBackPressed() { 
    super.onBackPressed(); 
    if (screen_Name != 0 && screen_Name == Constants.TAG_WIDGET_SCREEN) { 
     int tagID = getIntent().getIntExtra(Constants.TAG_ID, -1); 
     int tagcount = getIntent().getIntExtra(Constants.TAG_COUNT, 0); 
     if (tagcount > 0) { 
     mDBAdapter.updateTagCountToZero(tagID); 
     } 
     PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.NEWS + type); 
     PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.CARD_PREF + cardPref); 

    } 
    } 

在onSavedInstance

@Override 
    protected void onSaveInstanceState(Bundle outState) { 

    if (feedList != null && feedList.size() > 0) { 
     outState.putSerializable(FEED_DATA, feedList); 
    } 
    stopProgressBar(); // stopping progress dialog 
    super.onSaveInstanceState(outState); 
    } 

请帮忙如何处理这种情况。

+0

我只是随机猜测,但是你尝试在方法的末尾放置'''super.onBackPressed();'''也许这triggeres已经像onSaveInstanceState – Maximosaic

+0

但这不是转载。这是实时apk。所以我不知道确切的原因。 –

回答

0

为了避免这个问题,你可以检查你的活动结束:

@Override 
public void onBackPressed() { 
    if (!isFinishing()) { 
     super.onBackPressed(); 
     if (screen_Name != 0 && screen_Name == Constants.TAG_WIDGET_SCREEN) { 
      int tagID = getIntent().getIntExtra(Constants.TAG_ID, -1); 
      int tagcount = getIntent().getIntExtra(Constants.TAG_COUNT, 0); 
      if (tagcount > 0) { 
       mDBAdapter.updateTagCountToZero(tagID); 
      } 
      PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.NEWS + type); 
      PreferenceUtils.getInstance(NewsActivity.this).remove(Constants.CARD_PREF + cardPref); 
     } 
    } 
} 
0
Super.onBackPressed() 

将调用你的活动结束。如果你想完成活动,首先执行你需要的,然后在最后调用它。

+0

嗨,请看日志,异常发生在FragmentManagerImpl.checkStateLoss(MyApplication:1533)。 –

+0

我在super.backPressed()之后只做了SharedPreference相关的任务。如果你在流中看到onOptionsItemSelected() - > onBackPressed - > ... checkStateLoss()和Exception。所以我认为异常发生在Preference任务之前。 –

+0

可以将super.onBackpressed()方法调用移动到onBackPressed()方法的结尾处​​吗?正如我所提到的,super.onBackPressed()将触发完成活动。 – 7383

相关问题