gpt4 book ai didi

java - 事件总线订阅问题(Otto - Guava 事件总线)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:58 24 4
gpt4 key购买 nike

为什么没有订阅事件,也没有调用 onMyEvent。相关代码如下。Otto 在我的使用中正常工作,但下面的场景涉及从回调处理程序发布事件(这是涉及 http 调用的代码的简化)。不确定这与它有什么关系。

我使用 Otto (基于 Guava 事件总线)和 Dagger (吉斯)。希望 Java 专家也能看到我在下面如何注入(inject)和使用总线的任何问题。

申请(模块注册)

package com.example.ottocb;

import android.app.Application;
import android.content.Context;
import com.squareup.otto.Bus;
import dagger.Module;
import dagger.ObjectGraph;
import dagger.Provides;

import javax.inject.Singleton;

public class MyApplication extends Application {
private ObjectGraph objectGraph;

@Override
public void onCreate() {
super.onCreate();
objectGraph = ObjectGraph.create(new MyModule(this));
}

public ObjectGraph objectGraph() {
return objectGraph;
}

public void inject(Object object) {
objectGraph.inject(object);
}

@Module(entryPoints = {Bus.class, MyActivity.class,
MyFragment.class
})
static class MyModule {
private final Context appContext;

MyModule(Context appContext) {
this.appContext = appContext;
}

@Provides
@Singleton
Bus provideBus() {
return new Bus();
}
}
}

基础 fragment

package com.example.ottocb;

import android.app.Fragment;
import android.os.Bundle;

public class BaseFragment extends Fragment {
@Override
public void onCreate(Bundle state) {
super.onCreate(state);

((MyApplication) getActivity()
.getApplication())
.inject(this);
}

}

我的 fragment

package com.example.ottocb;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import javax.inject.Inject;

public class MyFragment extends BaseFragment {
private static final String TAG = MyFragment.class.getName();

@Inject
Bus bus;

Button btn;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment, container,
false);
btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(btnOnClick);

return view;
}

Button.OnClickListener btnOnClick = new Button.OnClickListener() {

@Override
public void onClick(View v) {
Log.i(TAG, "onClick");
MyCB cb = new MyCB();
cb.success();
}
};

private class MyCB {
public void success() {
Log.i(TAG, "SUCCESS " );

bus.post(new MyEvent());

}

public void failure() {
Log.e(TAG, "Error");

}
}

@Subscribe
public void onMyEvent(MyEvent event) {
Log.i(TAG, "***** onMyEvent ********");
Toast.makeText(getActivity(), "***** RECEIVED EVENT *****", Toast.LENGTH_SHORT).show();
}
}

我的 Activity

package com.example.ottocb;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

我的 Activity

package com.example.ottocb;

public class MyEvent {
}

主.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.ottocb.MyFragment"
android:id="@+id/myfragment"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

我的 fragment .xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post" />


</LinearLayout>

最佳答案

为了接收事件,类实例需要在总线上注册。

 bus.register(this); 

关于java - 事件总线订阅问题(Otto - Guava 事件总线),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14648151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com