package com.nestedworld.nestedworld.activities.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import butterknife.ButterKnife;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/**
 * Abstract class for AppCompatActivity
 * it make a little abstraction of the life cycle
 * it add a butterKnife support and some useful field/method
 */
public abstract class BaseAppCompatActivity extends AppCompatActivity {
    protected final String TAG = getClass().getSimpleName();

    /*
    ** Method that every child will have to implement
     */

    /**
     * get the layout id
     * it will be use under onCreate()
     * as setContentView(getLayoutResource());
     */
    protected abstract int getLayoutResource();

    /**
     * init the activity here
     * This method is equivalent to onCreate()
     */
    protected abstract void init(Bundle savedInstanceState);

    /*
    ** Life cycle
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getLayoutResource());
        ButterKnife.bind(this);
        init(savedInstanceState);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
                break;
        }
        return (super.onOptionsItemSelected(menuItem));
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        //We override the font
        //See https://github.com/chrisjenx/Calligraphy
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    /*
    ** Utils
     */
    public String toString() {
        return TAG;
    }

    protected void startActivity(Class clazz, Bundle bundle) {
        try {
            final Intent intent = new Intent(this, clazz);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void startActivity(Class clazz) {
        startActivity(clazz, null);
    }
}

@@ -164,31 +164,103 @@
 rt.v
-7.app.AppCompatActivity
+4.app.FragmentManager;%0Aimport android.support.v7.app.AppCompatActivity;%0Aimport android.util.Log
 ;%0Aim
@@ -1516,100 +1516,321 @@
     
-onBackPressed();%0A                break;%0A            default:%0A                break;%0A
+Log.e(TAG, %22%22+ getSupportFragmentManager().getBackStackEntryCount());%0A                if (getSupportFragmentManager().getBackStackEntryCount() %3E 0) %7B%0A                    getSupportFragmentManager().popBackStack();%0A                %7D%0A                else %7B%0A                    super.onBackPressed();%0A       
         
 %7D%0A  
@@ -1817,34 +1817,93 @@
 %0A               
+ 
 %7D%0A
+                return true;%0A            default:%0A        
         return (
@@ -1938,24 +1938,34 @@
 menuItem));%0A
+        %7D%0A
     %7D%0A%0A    @

solve bug with fragment stack