package fr.o80.sample.lib.core.ui;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import fr.o80.sample.lib.core.presenter.Presenter;
import fr.o80.sample.lib.core.presenter.PresenterView;

/**
 * Base Fragment for every
 *
 * @author Olivier Perez
 */
public abstract class BaseFragment extends Fragment implements PresenterView {

    private Unbinder unbinder;
    private Presenter presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        unbinder.unbind();
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter = presenter();
        if (presenter != null) {
            presenter.attach(this);
        }
    }

    @Override
    public void onPause() {
        if (presenter != null) {
            presenter.dettach();
        }
        super.onPause();
    }

    @LayoutRes
    protected abstract int getLayoutId();

    protected abstract Presenter presenter();

}

@@ -58,16 +58,48 @@
 agment;%0A
+import android.content.Context;%0A
 import a
@@ -297,16 +297,68 @@
 ewGroup;
+%0Aimport android.view.inputmethod.InputMethodManager;
 %0A%0Aimport
@@ -696,41 +696,8 @@
 der;
-%0A    private Presenter presenter;
 %0A%0A  
@@ -956,24 +956,25 @@
  view);%0A
+%0A
         
 return v
@@ -969,132 +969,142 @@
     
+P
 re
-turn view
+senter presenter = presenter()
 ;%0A    
-%7D%0A%0A
     
-@Override%0A    public void onDestroyView() %7B%0A        unbinder.unbind();%0A        super.onDestroyV
+if (presenter != null) %7B%0A            presenter.attach(this);%0A        %7D%0A%0A        return v
 iew
-()
 ;%0A  
@@ -1140,22 +1140,27 @@
  void on
-Resume
+DestroyView
 () %7B%0A   
@@ -1168,33 +1168,17 @@
     
-super.onResume();%0A       
+Presenter
  pre
@@ -1250,35 +1250,32 @@
   presenter.
-a
+de
 ttach(
-this
 );%0A        %7D
@@ -1279,56 +1279,123 @@
   %7D%0A
+%0A
     
-%7D%0A%0A
     
-@Override%0A    public void onPause() %7B
+super.onDestroyView();%0A        unbinder.unbind();%0A%0A        // Hide keyboard%0A        View view = getView();
 %0A   
@@ -1399,33 +1399,28 @@
         if (
-presenter
+view
  != null) %7B%0A
@@ -1435,34 +1435,121 @@
     
-presenter.dettach(
+InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE
 );%0A     
    %7D
@@ -1548,37 +1548,79 @@
     
-   %7D%0A
        
- super.onPause();
+imm.hideSoftInputFromWindow(view.getWindowToken(), 0);%0A        %7D
 %0A   

Improve BaseFragment