我正在学习使用 otto 作为活动系统。我可以注册我的活动并从{{1}}收到消息。但是,如果我注册我的bus.post()
,则不会调用订阅的方法。我做错了什么?
这是我的活动:
Application class
App class
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);
}
}
我的NetModule(部分)
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;
}
}
NetScope
@Module
public class NetModule {
public NetModule() {
}
@Provides
@NetScope
BusWorker provideBusWorker(){
return new BusWorker();
}
}
我的NetComponent
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface NetScope {
}
答案 0 :(得分:0)
我修改了组件,模块以及它们在Application类中初始化的方式。这是在Application,Activity和fragment中收到正确消息的工作代码:
SchoolComponent
@SchoolScope
@Component(modules={SchoolModule.class})
public interface SchoolComponent {
void inject(MainActivity activity);
void inject(FragmentTest fragment);
void inject(App app);
School provideSchool();
}
NetComponent
@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();
}
NetModule
@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();
}
}
申请类
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;
}
}
MainActivity
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);
}
}