package com.davidstemmer.screenplay.scene;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import mortar.Blueprint;
import mortar.Mortar;
import mortar.MortarScope;

/**
 * Created by weefbellington on 10/20/14.
 */
public abstract class ScopedScene extends StandardScene implements Blueprint {

    private MortarScope scope;

    protected ScopedScene() {
    }

    protected ScopedScene(Component... components) {
        super(components);
    }

    @Override
    public View setUp(Context context, ViewGroup parent) {
        MortarScope parentScope = Mortar.getScope(context);
        scope = parentScope.requireChild(this);
        Context childContext = scope.createContext(context);
        return super.setUp(childContext, parent);
    }

    @Override
    public View tearDown(Context context, ViewGroup parent) {
        MortarScope parentScope = Mortar.getScope(context);
        parentScope.destroyChild(scope);
        scope = null;
        return super.tearDown(context, parent);
    }
}

package com.davidstemmer.screenplay.scene;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import mortar.Blueprint;
import mortar.Mortar;
import mortar.MortarScope;

/**
 * Created by weefbellington on 10/20/14.
 */
public abstract class ScopedScene extends StandardScene implements Blueprint {

    private MortarScope scope;

    @Override
    public View setUp(Context context, ViewGroup parent) {
        return super.setUp(createScope(context), parent);
    }

    @Override
    public View tearDown(Context context, ViewGroup parent) {
        View destroyed = super.tearDown(context, parent);
        destroyScope(context);
        return destroyed;
    }


    private Context createScope(Context context) {
        MortarScope parentScope = Mortar.getScope(context);
        scope = parentScope.requireChild(this);
        Context childContext = scope.createContext(context);

        Mortar.inject(childContext, this);
        onCreateScope(scope);
        return childContext;
    }

    private void destroyScope(Context context) {
        MortarScope parentScope = Mortar.getScope(context);
        parentScope.destroyChild(scope);
        scope = null;
        onDestroyScope();
    }

    public void onCreateScope(MortarScope scope) {}

    public void onDestroyScope() {}
}

update the ScopedScene to also call Mortar.inject. Add callback methods so that subclasses know when the scope has been created and injection has occured.