AppCompatActivity 如何使用 EventBus 与 FragmentActivity 通信?
How can an AppCompatActivity communicate with FragmentActivity using the EventBus?
问题,AppCompatActivity如何使用EventBus与FragmentActivity通信?
发现,FragmentActivity可以和AppCompatActivity通信,并且会调用onEvent方法,但是如果我们把通信路径切换到AppCompatActivity和FragmentActivity通信,onEvent方法永远不会被调用。
public class MainActivity extends AppCompatActivity{
private String data;
@Override protected void onCreate{
data = "private String data from MainActivity";
EventBus.getDefault().postSticky(data);
}
}
public class AccountFade extends FragmentActivity{
private String mAccountFadeData;
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(String s){
Toast.makeText(this, "private String mAccountFadeData from MainActivity", Toast.LENGTH_LONG).show();
this.mAccountFadeData = s;
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
@Override public void onDestroy(){
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
您必须创建包含一个字符串变量的 class 并通过 EventBus 触发 class。
所以首先像这样创建 class
public class DataChangeEvent {
private String data;
public DataChangeEvent(String data) {
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
现在将它用于您的活动
Post 在活动中:
EventBus.getDefault().postSticky(new DataChangeEvent(data));
进入FragmentActivity :
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(DataChangeEvent event){
Toast.makeText(this, "private String mAccountFadeData from MainActivity", Toast.LENGTH_LONG).show();
this.mAccountFadeData = event.getData();
}
问题,AppCompatActivity如何使用EventBus与FragmentActivity通信?
发现,FragmentActivity可以和AppCompatActivity通信,并且会调用onEvent方法,但是如果我们把通信路径切换到AppCompatActivity和FragmentActivity通信,onEvent方法永远不会被调用。
public class MainActivity extends AppCompatActivity{
private String data;
@Override protected void onCreate{
data = "private String data from MainActivity";
EventBus.getDefault().postSticky(data);
}
}
public class AccountFade extends FragmentActivity{
private String mAccountFadeData;
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(String s){
Toast.makeText(this, "private String mAccountFadeData from MainActivity", Toast.LENGTH_LONG).show();
this.mAccountFadeData = s;
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
@Override public void onDestroy(){
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
您必须创建包含一个字符串变量的 class 并通过 EventBus 触发 class。
所以首先像这样创建 class
public class DataChangeEvent {
private String data;
public DataChangeEvent(String data) {
this.data = data;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
现在将它用于您的活动
Post 在活动中:
EventBus.getDefault().postSticky(new DataChangeEvent(data));
进入FragmentActivity :
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(DataChangeEvent event){
Toast.makeText(this, "private String mAccountFadeData from MainActivity", Toast.LENGTH_LONG).show();
this.mAccountFadeData = event.getData();
}