/*
* Copyright (C) 2014 Andrew Reitz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.andrewreitz.encryptedcamera.ui.activity;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import com.andrewreitz.encryptedcamera.EncryptedCameraApp;
import com.andrewreitz.encryptedcamera.di.module.ActivityModule;
import java.util.Arrays;
import java.util.List;
import butterknife.ButterKnife;
import dagger.ObjectGraph;
public abstract class BaseActivity extends Activity {
private ObjectGraph mActivityGraph;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inject objects into the object graph at the activity level, this is for
// objects that need values that aren't available until the activity is created.
EncryptedCameraApp application = EncryptedCameraApp.get(this);
mActivityGraph = application
.getApplicationGraph()
.plus(
getModules().toArray()
);
mActivityGraph.inject(this);
}
@Override
protected void onDestroy() {
// Eagerly clear the reference to the activity graph to allow
// it to be garbage collected as soon as possible.
mActivityGraph = null;
super.onDestroy();
}
protected List<Object> getModules() {
return Arrays.<Object>asList(
new ActivityModule(this)
);
}
/** Inject the supplied {@code object} using the activity-specific graph. */
public void inject(Object object) {
mActivityGraph.inject(object);
}
public static BaseActivity get(Fragment fragment) {
return (BaseActivity) fragment.getActivity();
}
}