package com.naz.sdkdemo.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import com.lzy.okgo.OkGo;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
/**
 * Description:
 * Author: yzxzm
 * Date:2020/12/28
 */
public abstract class BaseFragment extends Fragment {
    public View mRootView;
    protected Bundle bundle;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getArguments();
        if (bundle == null) {
            bundle = new Bundle();
        }
        initBundle(bundle);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mRootView != null) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null)
                parent.removeView(mRootView);
        } else {
            mRootView = inflater.inflate(getLayoutId(), container, false);
            // Do something
            onBindViewBefore(mRootView);
            // Bind view
            ButterKnife.bind(this, mRootView);
            // Get savedInstanceState
            if (savedInstanceState != null)
                onRestartInstance(savedInstanceState);
            // Init
            initView();
            initData();
        }
        return mRootView;
    }
    protected void initBundle(Bundle bundle) {
    }

    protected abstract int getLayoutId();

    protected void onBindViewBefore(View root) {
    }

    protected abstract void initView();

    protected abstract void initData();

    protected void onRestartInstance(Bundle bundle) {
    }

    //进行跳转
    public void GoPath(Class c) {
        Intent intent = new Intent(getActivity(), c);
        startActivity(intent);
    }

    public void GoPath(Class c, Bundle bundle) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onPause() {
        super.onPause();
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
    }

    @Override
    public void onDestroy() {
        OkGo.getInstance().cancelTag(this);
        bundle.clear();
        bundle = null;
        super.onDestroy();
    }
}

