Last active
May 15, 2018 04:40
-
-
Save aucd29/46f3931f5c9329a5aeff51555f02ac3d to your computer and use it in GitHub Desktop.
dagger 예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Bk { | |
@Inject | |
public Bk() { | |
} | |
public String helloworld() { | |
return "Hello world"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends DaggerAppCompatActivity { | |
@Inject | |
Bk mBk; // 알아서 instance 됨 | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
MainViewModel vm = ViewModelProviders.of(this).get(MainViewModel.class); | |
vm.netInfo.set(mBk.helloworld()); | |
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
binding.setModel(vm); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainApp extends DaggerApplication { | |
@Override | |
protected AndroidInjector<? extends DaggerApplication> applicationInjector() { | |
return DaggerMyAppComponent.builder().application(this).build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component( modules = { AndroidInjectionModule.class, MyAppModule.class }) | |
public interface MyAppComponent extends AndroidInjector<MainApp> { | |
@Component.Builder | |
interface Builder { | |
@BindsInstance | |
MyAppComponent.Builder application(Application application); | |
MyAppComponent build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public abstract class MyAppModule { | |
@ContributesAndroidInjector | |
abstract MainActivity contributeActivityInjector(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment