package com.jess.arms.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.jess.arms.base.delegate.IActivity;
import com.jess.arms.mvp.IPresenter;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.jess.arms.utils.ThirdViewUtil.convertAutoView;

/**
 * 因为java只能单继承,所以如果有需要继承特定Activity的三方库,那你就需要自己自定义Activity
 * 继承于这个特定的Activity,然后按照将BaseActivity的格式,复制过去记住一定要实现{@link IActivity}
 */
public abstract class BaseActivity<P extends IPresenter> extends RxAppCompatActivity implements IActivity {
    protected final String TAG = this.getClass().getSimpleName();
    private Unbinder mUnbinder;
    @Inject
    protected P mPresenter;

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View view = convertAutoView(name, context, attrs);
        return view == null ? super.onCreateView(name, context, attrs) : view;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            int layoutResID = initView(savedInstanceState);
            if (layoutResID != 0)//如果initView返回0,框架则不会调用setContentView()
                setContentView(layoutResID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //绑定到butterknife
        mUnbinder = ButterKnife.bind(this);
        initData(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
        this.mUnbinder = null;
        if (mPresenter != null)
            mPresenter.onDestroy();//释放资源
        this.mPresenter = null;
    }

    /**
     * 是否使用eventBus,默认为使用(true)，
     *
     * @return
     */
    @Override
    public boolean useEventBus() {
        return true;
    }

    /**
     * 这个Activity是否会使用Fragment,框架会根据这个属性判断是否注册{@link android.support.v4.app.FragmentManager.FragmentLifecycleCallbacks}
     * 如果返回false,那意味着这个Activity不需要绑定Fragment,那你再在这个Activity中绑定继承于 {@link com.jess.arms.base.BaseFragment} 的Fragment将不起任何作用
     *
     * @return
     */
    @Override
    public boolean useFragment() {
        return true;
    }
}

@@ -1392,16 +1392,18 @@
 ID != 0)
+ %7B
 //%E5%A6%82%E6%9E%9Cinit
@@ -1433,16 +1433,39 @@
 ntView()
+,%E5%BD%93%E7%84%B6%E4%B9%9F%E4%B8%8D%E4%BC%9A Bind ButterKnife
 %0D%0A      
@@ -1516,147 +1516,178 @@
     
-%7D catch (Exception e) %7B%0D%0A            e.printStackTrace();%0D%0A        %7D%0D%0A        //%E7%BB%91%E5%AE%9A%E5%88%B0butterknife%0D%0A        mUnbinder = ButterKnife.bind(this);
+        //%E7%BB%91%E5%AE%9A%E5%88%B0butterknife%0D%0A                mUnbinder = ButterKnife.bind(this);%0D%0A            %7D%0D%0A        %7D catch (Exception e) %7B%0D%0A            e.printStackTrace();%0D%0A        %7D
 %0D%0A  

Improve BaseActivity