android LocalBroadcast 的两个接收者,一个在 MainActivity 中,另一个在片段中
Two receivers of android LocalBroadcast, one in MainActivity, the other in a fragment
在 android 应用中,我在 MainActivity 的 onCreate
中注册了一个接收器
IntentFilter mFilter = new IntentFilter("Action");
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, mFilter);
在其onResume
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent("Action");
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(i);
}
});
}
}).start();
坦率地说,我不确定为什么我们要这样使用线程(我从某个地方复制了代码 w/o 完全消化了它)。
此应用支持 ViewPager,因此在其关联的 Fragment 的 onCreate
IntentFilter mFilter = new IntentFilter("Action");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, mFilter);
在 MainActivity 和 Fragment class 中,接收者看起来像:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
只有 onReceive 中的内容在两个 class 中不同。
我不太了解 LocalBroadcast 是如何工作的,我原以为一旦广播发出,两个接收器处理程序都会 运行。相反,我注意到大多数时间只有 MainActivity 运行s 中的接收器,偶尔在 class 运行s.
片段中
我的预感是线程部分有问题。
这种行为背后的原因可能是 Activity 和 Fragment 的生命周期:
根据我的经验,当你有 Activity+Fragment 时如何调用这些方法是:
- Activity的
onCreate()
- Activity的
onStart()
- Activity的
onResume()
- 片段的
onCreateView()
- 片段的
onStart()
- 片段的
onResume()
Explanation :
As your Fragment is not initialized yet when you are broadcasting from
onResume()
in Activity it could not be received at first by The
Fragment but only receive by Activity. After that once the fragment is
initialized the Broadcast will be received by the Fragment also.
在 android 应用中,我在 MainActivity 的 onCreate
中注册了一个接收器IntentFilter mFilter = new IntentFilter("Action");
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, mFilter);
在其onResume
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent("Action");
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(i);
}
});
}
}).start();
坦率地说,我不确定为什么我们要这样使用线程(我从某个地方复制了代码 w/o 完全消化了它)。
此应用支持 ViewPager,因此在其关联的 Fragment 的 onCreate
IntentFilter mFilter = new IntentFilter("Action");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, mFilter);
在 MainActivity 和 Fragment class 中,接收者看起来像:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
只有 onReceive 中的内容在两个 class 中不同。
我不太了解 LocalBroadcast 是如何工作的,我原以为一旦广播发出,两个接收器处理程序都会 运行。相反,我注意到大多数时间只有 MainActivity 运行s 中的接收器,偶尔在 class 运行s.
片段中我的预感是线程部分有问题。
这种行为背后的原因可能是 Activity 和 Fragment 的生命周期:
根据我的经验,当你有 Activity+Fragment 时如何调用这些方法是:
- Activity的
onCreate()
- Activity的
onStart()
- Activity的
onResume()
- 片段的
onCreateView()
- 片段的
onStart()
- 片段的
onResume()
Explanation :
As your Fragment is not initialized yet when you are broadcasting from
onResume()
in Activity it could not be received at first by The Fragment but only receive by Activity. After that once the fragment is initialized the Broadcast will be received by the Fragment also.