OTTO + Dagger - 无法在应用程序 class 上获取事件消息
OTTO + Dagger - Can't get event message on Application class
我正在学习使用 otto 作为事件系统。我可以注册我的 activity 并接收来自 bus.post()
的消息。但是,如果我注册 Application class
,则不会调用订阅的方法。我做错了什么?
这是我的 activity:
public class MainActivity extends AppCompatActivity {
@Inject
BusWorker busWorker;
@Inject
LogWorker logWorker;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
inject();
ButterKnife.bind(this);
busWorker.register(this);
binding.setHandlers(this);
}
@Subscribe
public void recievedMessage(Message message) {
logWorker.log("recievedMessage: " + message.getMessage());
}
public void onClickButton(View view) {
switch (view.getId()) {
case R.id.button:
busWorker.post("Test message");
break;
default:
break;
}
}
@Override
protected void onStop() {
super.onStop();
busWorker.unRegister(this);
}
void inject() {
((App) getApplication()).getGeneralComponent().inject(this);
((App) getApplication()).getSchoolComponent().inject(this);
}
}
应用class
public class App extends Application {
private NetComponent netComponent;
private GeneralComponent generalComponent;
@Inject
public App() {}
@Override
public void onCreate() {
super.onCreate();
netComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this))
.netModule(new NetModule())
.build();
generalComponent = DaggerGeneralComponent.builder()
.netComponent(netComponent)
.generalModule(new GeneralModule())
.build();
netComponent.BusWorker().register(this);
}
@Subscribe
public void recievedMessage(Message message) {
netComponent.logWorker().log("recievedMessage: " + message.getMessage());
}
public GeneralComponent getGeneralComponent() {
return generalComponent;
}
public NetComponent getNetComponent() {
return netComponent;
}
}
我的网络模块(部分)
@Module
public class NetModule {
public NetModule() {
}
@Provides
@NetScope
BusWorker provideBusWorker(){
return new BusWorker();
}
}
NetScope
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface NetScope {
}
我的网络组件
@NetScope
@Component(modules={AppModule.class, NetModule.class})
public interface NetComponent {
NetWorker netWorker();
DbWorker dbWorker();
LogWorker logWorker();
SharedPreferencesWorker sharedPreferences();
BusWorker BusWorker();
}
我修改了组件、模块以及它们在应用程序中的初始化方式 class。这是在应用程序中收到正确消息的工作代码,Activity 和片段:
学校组件
@SchoolScope
@Component(modules={SchoolModule.class})
public interface SchoolComponent {
void inject(MainActivity activity);
void inject(FragmentTest fragment);
void inject(App app);
School provideSchool();
}
网络组件
@NetScope
@Component(modules={NetModule.class})
public interface NetComponent {
void inject(MainActivity activity);
void inject(FragmentTest fragment);
void inject(App app);
NetWorker provideNetWorker();
DbWorker provideDbWorker();
LogWorker provideLogWorker();
SharedPreferencesWorker provideSharedPreferences();
}
网络模块
@Module
public class NetModule {
@Provides
@NetScope
SharedPreferencesWorker provideSharedPreferences(){
return new SharedPreferencesWorker();
}
@Provides
@NetScope
NetWorker provideNetWorker(){
return new NetWorker();
}
@Provides
@NetScope
DbWorker provideDbWorker(){
return new DbWorker();
}
@Provides
@NetScope
LogWorker provideLogWorker(){
return new LogWorker();
}
@Provides
@NetScope
BusWorker provideBusWorker(){
return new BusWorker();
}
}
申请class
public class App extends Application {
private NetComponent netComponent;
private SchoolComponent schoolComponent;
@Inject
BusWorker busWorker;
@Inject
public App() {
}
@Override
public void onCreate() {
super.onCreate();
schoolComponent = DaggerSchoolComponent.create();
schoolComponent.inject(this);
netComponent = DaggerNetComponent.create();
netComponent.inject(this);
busWorker.register(this);
}
@Subscribe
public void recievedMessage(Message message) {
Log.d("Dagger", "recievedMessage App: " + message.getMessage());
}
public SchoolComponent getSchoolComponent() {
return schoolComponent;
}
public NetComponent getNetComponent() {
return netComponent;
}
}
主要Activity
public class MainActivity extends AppCompatActivity {
@Inject
BusWorker busWorker;
@Inject
LogWorker logWorker;
@Inject
School school;
@Bind(R.id.name)
TextView name;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
ButterKnife.bind(this);
inject();
}
@Override
protected void onResume() {
super.onResume();
busWorker.register(this);
}
@Override
protected void onPause() {
super.onPause();
busWorker.unRegister(this);
}
@Subscribe
public void recievedMessage(Message message) {
logWorker.log("recievedMessage Activity: " + message.getMessage());
}
public void onClickButton(View view) {
switch (view.getId()) {
case R.id.button:
busWorker.post(new Message("blablabla"));
break;
default:
break;
}
}
void inject() {
((App) getApplication()).getNetComponent().inject(this);
((App) getApplication()).getSchoolComponent().inject(this);
}
}
我正在学习使用 otto 作为事件系统。我可以注册我的 activity 并接收来自 bus.post()
的消息。但是,如果我注册 Application class
,则不会调用订阅的方法。我做错了什么?
这是我的 activity:
public class MainActivity extends AppCompatActivity {
@Inject
BusWorker busWorker;
@Inject
LogWorker logWorker;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
inject();
ButterKnife.bind(this);
busWorker.register(this);
binding.setHandlers(this);
}
@Subscribe
public void recievedMessage(Message message) {
logWorker.log("recievedMessage: " + message.getMessage());
}
public void onClickButton(View view) {
switch (view.getId()) {
case R.id.button:
busWorker.post("Test message");
break;
default:
break;
}
}
@Override
protected void onStop() {
super.onStop();
busWorker.unRegister(this);
}
void inject() {
((App) getApplication()).getGeneralComponent().inject(this);
((App) getApplication()).getSchoolComponent().inject(this);
}
}
应用class
public class App extends Application {
private NetComponent netComponent;
private GeneralComponent generalComponent;
@Inject
public App() {}
@Override
public void onCreate() {
super.onCreate();
netComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this))
.netModule(new NetModule())
.build();
generalComponent = DaggerGeneralComponent.builder()
.netComponent(netComponent)
.generalModule(new GeneralModule())
.build();
netComponent.BusWorker().register(this);
}
@Subscribe
public void recievedMessage(Message message) {
netComponent.logWorker().log("recievedMessage: " + message.getMessage());
}
public GeneralComponent getGeneralComponent() {
return generalComponent;
}
public NetComponent getNetComponent() {
return netComponent;
}
}
我的网络模块(部分)
@Module
public class NetModule {
public NetModule() {
}
@Provides
@NetScope
BusWorker provideBusWorker(){
return new BusWorker();
}
}
NetScope
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface NetScope {
}
我的网络组件
@NetScope
@Component(modules={AppModule.class, NetModule.class})
public interface NetComponent {
NetWorker netWorker();
DbWorker dbWorker();
LogWorker logWorker();
SharedPreferencesWorker sharedPreferences();
BusWorker BusWorker();
}
我修改了组件、模块以及它们在应用程序中的初始化方式 class。这是在应用程序中收到正确消息的工作代码,Activity 和片段:
学校组件
@SchoolScope
@Component(modules={SchoolModule.class})
public interface SchoolComponent {
void inject(MainActivity activity);
void inject(FragmentTest fragment);
void inject(App app);
School provideSchool();
}
网络组件
@NetScope
@Component(modules={NetModule.class})
public interface NetComponent {
void inject(MainActivity activity);
void inject(FragmentTest fragment);
void inject(App app);
NetWorker provideNetWorker();
DbWorker provideDbWorker();
LogWorker provideLogWorker();
SharedPreferencesWorker provideSharedPreferences();
}
网络模块
@Module
public class NetModule {
@Provides
@NetScope
SharedPreferencesWorker provideSharedPreferences(){
return new SharedPreferencesWorker();
}
@Provides
@NetScope
NetWorker provideNetWorker(){
return new NetWorker();
}
@Provides
@NetScope
DbWorker provideDbWorker(){
return new DbWorker();
}
@Provides
@NetScope
LogWorker provideLogWorker(){
return new LogWorker();
}
@Provides
@NetScope
BusWorker provideBusWorker(){
return new BusWorker();
}
}
申请class
public class App extends Application {
private NetComponent netComponent;
private SchoolComponent schoolComponent;
@Inject
BusWorker busWorker;
@Inject
public App() {
}
@Override
public void onCreate() {
super.onCreate();
schoolComponent = DaggerSchoolComponent.create();
schoolComponent.inject(this);
netComponent = DaggerNetComponent.create();
netComponent.inject(this);
busWorker.register(this);
}
@Subscribe
public void recievedMessage(Message message) {
Log.d("Dagger", "recievedMessage App: " + message.getMessage());
}
public SchoolComponent getSchoolComponent() {
return schoolComponent;
}
public NetComponent getNetComponent() {
return netComponent;
}
}
主要Activity
public class MainActivity extends AppCompatActivity {
@Inject
BusWorker busWorker;
@Inject
LogWorker logWorker;
@Inject
School school;
@Bind(R.id.name)
TextView name;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
ButterKnife.bind(this);
inject();
}
@Override
protected void onResume() {
super.onResume();
busWorker.register(this);
}
@Override
protected void onPause() {
super.onPause();
busWorker.unRegister(this);
}
@Subscribe
public void recievedMessage(Message message) {
logWorker.log("recievedMessage Activity: " + message.getMessage());
}
public void onClickButton(View view) {
switch (view.getId()) {
case R.id.button:
busWorker.post(new Message("blablabla"));
break;
default:
break;
}
}
void inject() {
((App) getApplication()).getNetComponent().inject(this);
((App) getApplication()).getSchoolComponent().inject(this);
}
}