首页 文章

java.lang.IllegalStateException:onSaveInstanceState On Activity Backpress后不能执行此操作(不使用任何片段)

提问于
浏览
1

在我的应用程序中,我得到java.lang.IllegalStateException:在onSaveInstanceState之后无法执行此操作 . 在活动的onBackPress()之后 .

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

请帮忙解决这个问题 .

2 回答

  • 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()
    

    将调用您的活动完成 . 如果要完成活动,请先执行所需操作,然后在结束时调用 .

相关问题