package com.marcelosmith77.android.easydrawer.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;

import com.marcelosmith77.android.easydrawer.activity.AbstractBaseDrawerActivity;
import com.marcelosmith77.android.easydrawer.utils.Utils;

public abstract class AbstractBaseDrawerFragment extends Fragment {

    /**
     * Shows fragment, adds it to the stack without clearing back stack
     *
     * @param fragment - The fragment
     */
    protected void showFrament(Fragment fragment) {

        showFrament(fragment, fragment.getClass().getSimpleName());
    }

    /**
     * Shows fragment, adds it to the stack without clearing back stack
     *
     * @param fragment - The fragment
     * @param name - The name for this back stack state
     */
    protected void showFrament(Fragment fragment, String name) {

        ((AbstractBaseDrawerActivity) getActivity()).showFrament(fragment, name, false, false);
    }

    /**
     * Shows fragment, without clearing back stack
     *
     * @param fragment - The fragment
     * @param addToStack - Add to stack?
     */
    protected void showFrament(Fragment fragment, boolean addToStack) {
       showFrament(fragment, fragment.getClass().getSimpleName(), addToStack);
    }

    /**
     * Shows fragment
     *
     * @param fragment - The fragment
     * @param addToStack - Add to stack?
     * @param clearStack - Clear stack?
     * @param key - parameter key
     * @param parcelable - fragment parameter
     */
    public void showFrament(Fragment fragment, boolean addToStack, boolean clearStack, String key, Parcelable parcelable) {
        Bundle args = new Bundle();
        args.putParcelable(key, parcelable);

        ((AbstractBaseDrawerActivity) getActivity()).showFrament(fragment, fragment.getClass().getSimpleName(), addToStack, clearStack, args);
    }

    /**
     * Shows fragment, without clearing back stack
     *
     * @param fragment - The fragment
     * @param name - The name for this back stack state
     * @param addToStack - Add to stack?
     */
    protected void showFrament(Fragment fragment, String name, boolean addToStack) {
        ((AbstractBaseDrawerActivity) getActivity()).showFrament(fragment, name, addToStack, false);
    }

    /**
     * Simules back button
'    */
    protected void back() {

        if (getActivity() != null)
            getActivity().onBackPressed(); // see AbstractBaseDrawerActivity

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Utils.hideKeyboard(getActivity());
            }
        });
    }

    /**
     * Checks activity signature
     * @param context - The activity context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (!(context instanceof AbstractBaseDrawerActivity)) {
            throw new RuntimeException(context.toString() + " must extends AbstractBaseDrawerActivity");
        }
    }
}




@@ -169,32 +169,80 @@
 v4.app.Fragment;
+%0Aimport android.support.v4.app.FragmentActivity;
 %0A%0Aimport com.mar
@@ -368,16 +368,44 @@
 Utils;%0A%0A
+import java.util.Optional;%0A%0A
 public a
@@ -977,24 +977,139 @@
 ng name) %7B%0A%0A
+        FragmentActivity activity = getActivity();%0A%0A        if (activity != null && !activity.isFinishing()) %7B%0A    
         ((Ab
@@ -1130,37 +1130,32 @@
 erActivity) 
-getA
+a
 ctivity
-()
 ).showFramen
@@ -1179,32 +1179,42 @@
  false, false);%0A
+        %7D%0A
     %7D%0A%0A    /**%0A 
@@ -1879,24 +1879,140 @@
 rcelable) %7B%0A
+%0A        FragmentActivity activity = getActivity();%0A%0A        if (activity != null && !activity.isFinishing()) %7B%0A    
         Bund
@@ -2035,16 +2035,20 @@
 ndle();%0A
+    
         
@@ -2085,32 +2085,36 @@
 able);%0A%0A        
+ 
+   
 ((AbstractBaseDr
@@ -2123,37 +2123,32 @@
 erActivity) 
-getA
+a
 ctivity
-()
 ).showFramen
@@ -2227,16 +2227,26 @@
  args);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2534,24 +2534,139 @@
 dToStack) %7B%0A
+        FragmentActivity activity = getActivity();%0A%0A        if (activity != null && !activity.isFinishing()) %7B%0A    
         ((Ab
@@ -2691,29 +2691,24 @@
 tivity) 
-getA
+a
 ctivity
-()
 ).showFr
@@ -2745,24 +2745,34 @@
 ck, false);%0A
+        %7D%0A
     %7D%0A%0A    /
@@ -2850,34 +2850,110 @@
     
-if (getActivity() != null)
+FragmentActivity activity = getActivity();%0A%0A        if (activity != null && !activity.isFinishing()) %7B
 %0A   
@@ -2961,29 +2961,24 @@
         
-getA
+a
 ctivity
-()
 .onBackP
@@ -3030,29 +3030,28 @@
         
+    
-getA
+a
 ctivity
-()
 .runOnUi
@@ -3082,24 +3082,28 @@
             
+    
 @Override%0A  
@@ -3108,24 +3108,28 @@
             
+ 
+   
 public void 
@@ -3132,24 +3132,28 @@
 oid run() %7B%0A
+    
             
@@ -3199,26 +3199,34 @@
             
-%7D%0A
+    %7D%0A    
         %7D);%0A
@@ -3225,16 +3225,26 @@
     %7D);%0A
+        %7D%0A
     %7D%0A%0A 

Fix nullpointer