• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.example.android.simple;
18 
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.widget.TextView;
22 import dagger.Binds;
23 import dagger.android.AndroidInjector;
24 import dagger.android.support.DaggerAppCompatActivity;
25 import dagger.multibindings.ClassKey;
26 import dagger.multibindings.IntoMap;
27 import javax.inject.Inject;
28 
29 /**
30  * The main activity application. It can be injected with any binding from both {@link Component}
31  * and {@link dagger.example.android.simple.SimpleApplication.Component}.
32  */
33 public class MainActivity extends DaggerAppCompatActivity {
34   @dagger.Subcomponent
35   interface Component extends AndroidInjector<MainActivity> {
36 
37     @dagger.Subcomponent.Builder
38     abstract class Builder extends AndroidInjector.Builder<MainActivity> {}
39   }
40 
41   @dagger.Module(subcomponents = Component.class)
42   abstract class Module {
43 
44     @Binds
45     @IntoMap
46     @ClassKey(MainActivity.class)
bind(Component.Builder builder)47     abstract AndroidInjector.Factory<?> bind(Component.Builder builder);
48   }
49 
50   private static final String TAG = MainActivity.class.getSimpleName();
51 
52   @Inject @Model String model;
53 
54   @Inject
logInjection()55   void logInjection() {
56     Log.i(TAG, "Injecting " + MainActivity.class.getSimpleName());
57   }
58 
59   @Override
onCreate(Bundle savedInstanceState)60   protected void onCreate(Bundle savedInstanceState) {
61     super.onCreate(savedInstanceState);
62 
63     setContentView(R.layout.activity_main);
64 
65     TextView greeting = (TextView) findViewById(R.id.greeting);
66     String text = getResources().getString(R.string.welcome, model);
67     greeting.setText(text);
68   }
69 }
70