No View Found for ID when “Don't Keep Activities” Is Turned On

落爺英雄遲暮 提交于 2019-12-23 16:34:30

问题


My app was crashing intermittently after some inactivity. So i figured I wasn't storing things correctly. I turned on "Don't Keep Activities" to troubleshoot and now my app is crashing everywhere.

Stack trace: https://gist.github.com/hanleyhansen/6d41fee54b1e129b7922 This is the layout that goes missing: https://gist.github.com/hanleyhansen/73ace0c99ae675023e0f


回答1:


I think you are experiencing a likely symptom of Issue 19917. This bug exists in 3.2 and higher and was only fixed recently (4.2). This same fix hasn't made its way in to the support library, yet.

Check out comment 28 for a real fix: you'll want to edit your support library and recompile. edit v4/java/android/support/v4/app/FragmentManager.java

Bundle saveFragmentBasicState(Fragment f) {
    Bundle result = null;

    if (mStateBundle == null) {
        mStateBundle = new Bundle();
    }
    f.onSaveInstanceState(mStateBundle);
    if (!mStateBundle.isEmpty()) {
        result = mStateBundle;
        mStateBundle = null;
    }

    if (f.mView != null) {
        saveFragmentViewState(f);
    }
    if (f.mSavedViewState != null) {
        if (result == null) {
            result = new Bundle();
        }
        result.putSparseParcelableArray(
                FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
    }
    if (!f.mUserVisibleHint) {
        // Only add this if it's not the default value
        // @@@ BUG, result may not have been created, can be null!
        if (result == null) {
            result = new Bundle();
        }
        result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
    }

    return result;
}

If you dont feel up to the task and want to wait for Google to fix the support library here is another workaround Comment 8 for fix you can apply to all your fragments

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //first saving my state, so the bundle wont be empty.
        //http://code.google.com/p/android/issues/detail?id=19917
        outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
        super.onSaveInstanceState(outState);
    }

I've also come across transaction.commitAllowingStateLoss(); as a "fix" for this instead of transaction.commit();

Some other background info and workaround I've found that are related that helped me with fragment issues (esp. when nesting them)

  • TNR's answer to java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState with DialogFragment (decent workaround)
  • How to fix “Can not perform this action after onSaveInstanceState” on Android Fragments ]4 (very informative)
  • Chris Jenkins answer to Do fragments really need an empty constructor?


来源:https://stackoverflow.com/questions/17416263/no-view-found-for-id-when-dont-keep-activities-is-turned-on

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