将信息传递给暂停的 activity 以调用方法(不是 UI 更新)
Pass information to a paused activity to call a method (Not UI update)
场景:
Activity A 打开 Activity B。 Activity B 有一些过滤器,Activity A 的内容会根据这些过滤器发生变化。为了减少用户的等待时间,我想通知 Activity A 在 [=30= 有变化时立即开始获取内容] B.
在不更改 activity 结构的情况下实现此目标的最佳方法是什么?
最终使用了 LocalBroadcastManager。
在 Activity A 中注册 LocalBroadCastManager 并从 Activity B 发送消息。退出时注销 Activity A.
在Activity答:
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("CUSTOM-EVENT_NAME"));
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// process the message here
String data = intent.getStringExtra("intent-key")
}
};
在Activity B:
Intent intent = new Intent("CUSTOM-EVENT_NAME");
intent.putExtra("intent-key","data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
场景:
Activity A 打开 Activity B。 Activity B 有一些过滤器,Activity A 的内容会根据这些过滤器发生变化。为了减少用户的等待时间,我想通知 Activity A 在 [=30= 有变化时立即开始获取内容] B.
在不更改 activity 结构的情况下实现此目标的最佳方法是什么?
最终使用了 LocalBroadcastManager。 在 Activity A 中注册 LocalBroadCastManager 并从 Activity B 发送消息。退出时注销 Activity A.
在Activity答:
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("CUSTOM-EVENT_NAME"));
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// process the message here
String data = intent.getStringExtra("intent-key")
}
};
在Activity B:
Intent intent = new Intent("CUSTOM-EVENT_NAME");
intent.putExtra("intent-key","data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);