fragment 爬坑之旅

参考:
https://zhuanlan.zhihu.com/p/20660984

0.使用fragment时,注意事项:

  1. 最好要保留系统默认的无参构造,如果要传递参数,使用 fragment.setArguments来传,此种方式传递的参数,在 fragment 被回收重建时,可通过getArgument获取得到;
  1. 系统回收重建fragment时,会调用fragment的无参构造,这也是保留默认无参构造的原因;
  1. fragment可以简单理解为一个 布局,即,类似于LinearLayout这样,所以,默认它的事件可穿透的,如果不允许,可设置fragment 的布局 clickable为true;

1.嵌套 Fragment 遇到问题(使用嵌套fragment要特别注意)

Caused by: java.lang.IllegalStateException: Child FragmentManager was not destroyed and this fragment is not retaining instance

嵌套fragment就是fragment中,存在其他子fragment的情况,我们使用 getChildFragmentManager来添加子fragment,在内存不够时,回收时,处理不好就会报这个异常;

解决方法:
父fragment 的 onSaveInstanceState 移除掉 子fragment,下次子fragment完全重建;

 private void removeNeedFillFragment() {
        needFillFragment = (NeedFillFragment) getChildFragmentManager().findFragmentByTag(NeedFillFragment.class.getName());
        if (needFillFragment != null) {
            final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.remove(needFillFragment).commitAllowingStateLoss();  // 不让其缓存
            Logger.e("flow_center", "》》》》》移除子fragment");
        }
        needFillFragment = null;
    }

    /**
     * 移除子Fragment
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        removeNeedFillFragment();   // 回收时,先移除子fragment,再调用super
        super.onSaveInstanceState(outState);
    }

** 纠正错误:**
不能使用此方法,当Activity不可见时,onSaveXXX方法会执行,这个时候,就出现问题,nestFragment将消失;
应这样解决,采用findFragmentByTag这种形式来,存在,不管,不存在则添加

2.状态丢失 can not perform this action after onSaveInstance

(使用fragment,dialogFragment经常遇到此种问题)
想必这个问题,遇到的不少,请参考:
http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html

建议方式:

  1. 非常小心提交 commit,如在:在onActivityResult,千万不要提交;
  2. 回调方法中,提交先判断Activity的状态,如:isFinish();
  3. 使用 commitAllowStateLoss(),如果不需要维护状态,可使用此方法提交;

来自stackOverFlow的解决方法(代码量有点多):
使用自定义handler来记录activity,并使用自定义handler来提交;

public abstract class PauseHandler extends Handler {
    /**
     * Message Queue Buffer
     */
    final Vector messageQueueBuffer = new Vector();
    /**
     * Flag indicating the pause state
     */
    private boolean paused;
    /**
     * Resume the handler
     */
    final public void resume() {
        paused = false;
        while (messageQueueBuffer.size() > 0) {
            final Message msg = messageQueueBuffer.elementAt(0);
            messageQueueBuffer.removeElementAt(0);
            sendMessage(msg);
        }
    }

    /**
     * Pause the handler
     */
    final public void pause() {
        paused = true;
    }

    /**
     * Notification that the message is about to be stored as the activity is
     * paused. If not handled the message will be saved and replayed when the
     * activity resumes.
     *
     * @param message the message which optional can be handled
     * @return true if the message is to be stored
     */
    protected abstract boolean storeMessage(Message message);

    /**
     * Notification message to be processed. This will either be directly from
     * handleMessage or played back from a saved message when the activity was
     * paused.
     *
     * @param message the message to be handled
     */
    protected abstract void processMessage(Message message);

    /**
     * {@inheritDoc}
     */
    @Override
    final public void handleMessage(Message msg) {
        if (paused) {
            if (storeMessage(msg)) {
                Message msgCopy = new Message();
                msgCopy.copyFrom(msg);
                messageQueueBuffer.add(msgCopy);
            }
        } else {
            processMessage(msg);
        }
    }
}

使用:

    /**
     * Handler for this activity
     */
    public ConcreteTestHandler handler = new ConcreteTestHandler();

    /**
     * 执行网络请求
     */
    private void doNetwork() {
        NetWorkManager.request(...,
                new SimpleRequestCallback(null, false, true) {
                    @Override
                    public void onSuccess(final ResponseInfo info) {
                        super.onSuccess(info);
                        // 2.移除加载框 (修改:避免 异常 onSaveInstanceState 异常)
                        handler.sendMessage(handler.obtainMessage(MSG_WHAT, MSG_REMOVE_DIALOG));
      ....
      ....
    }

   /**
     * Used for "what" parameter to handler messages
     */
    final static int MSG_WHAT = 22;
    final static int MSG_REMOVE_DIALOG = 1;

    /**
     * Message Handler class that supports buffering up of messages when the
     * activity is paused i.e. in the background.
     */
    static class ConcreteTestHandler extends PauseHandler {
        /**
         * Activity instance
         */
        protected Activity activity;

        final void setActivity(Activity activity) {
            this.activity = activity;
        }

        @Override
        final protected boolean storeMessage(Message message) {
            return true;
        }


        @Override
        final protected void processMessage(Message msg) {
            final Activity activity = this.activity;
            if (activity != null) {
                switch (msg.what) {
                    case MSG_WHAT:
                        // 在这里提交
                        break;
                }
            }
        }
    }

3.fragment重建问题:

系统回收时,会保留fragment状态,如果在创建 activity时,再次创建fragment,就导致fragment重新创建;
解决就是,判断状态

@Override
protected void onCreate(Bundle savedInstance) {
   if (savedInstance == null) {
      // 创建fragment,并添加到其activity中
   }
}

你可能感兴趣的:(fragment 爬坑之旅)