关于活动和片段之间的数据交换有很多讨论,但我仍然对设计缺陷感到不满。
我有一项活动需要从服务中接收一些数据。当此数据发生更改时,服务会发送本地广播,活动需要获取最新数据集。
public class ActivityChannelConfig extends Activity implements FragmentChannelList.FragmentChanneListInferface, FragmentCustomChannelList.FragmentCustomChannelListInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Bind to Bluetooth Service
bindService(new Intent(this, ServiceBluetoothConnection.class),
mServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
ActivityChannelConfig activity = mActivity.get();
if(activity == null)
return;
ServiceBluetoothConnection.BTConnServiceBinder binder = (ServiceBluetoothConnection.BTConnServiceBinder) service;
mService = binder.getService();
mIsBound = true;
// Put current list into channel list fragment
FragmentCustomChannelList customChannelList = (FragmentCustomChannelList)activity.getFragmentManager().findFragmentByTag(activity.TAG_FRAGMENT_CUSTOMIZE);
if(customChannelList != null) {
customChannelList.setChannelList(activity.mServiceConnection.getService().getmDBMeasurement().getSortedMeasChannels(DBMeasurement.MEAS_CHANNEL_FLAG.MEAS_CHANNEL_ENABLED));
}
FragmentChannelList channelList = (FragmentChannelList)activity.getFragmentManager().findFragmentByTag(activity.TAG_FRAGMENT_LIST);
if(channelList != null) {
channelList.setChannelList(activity.mServiceConnection.getService().getmDBMeasurement().getSortedMeasChannels(DBMeasurement.MEAS_CHANNEL_FLAG.MEAS_CHANNEL_ENABLED));
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
final ActivityChannelConfig activity = mActivity.get();
// Bail out if the ActivityMarwisApp is gone.
if (activity == null)
return;
// Channel list has changed
if(intent.getAction().equals(DBMeasurement.INTENT_DB_CHANNEL_LIST_CHANGED)) {
// Put current list into channel list fragment
FragmentCustomChannelList customChannelList = (FragmentCustomChannelList)activity.getFragmentManager().findFragmentByTag(activity.TAG_FRAGMENT_CUSTOMIZE);
if(customChannelList != null) {
customChannelList.setChannelList(activity.mServiceConnection.getService().getmDBMeasurement().getSortedMeasChannels(DBMeasurement.MEAS_CHANNEL_FLAG.MEAS_CHANNEL_ENABLED));
}
FragmentChannelList channelList = (FragmentChannelList)activity.getFragmentManager().findFragmentByTag(activity.TAG_FRAGMENT_LIST);
if(channelList != null) {
channelList.setChannelList(activity.mServiceConnection.getService().getmDBMeasurement().getSortedMeasChannels(DBMeasurement.MEAS_CHANNEL_FLAG.MEAS_CHANNEL_ENABLED));
}
}
}
}
现在我的问题是我不知道,如何将这些数据初始化和动态地传递给我的活动中的片段。始终存在的问题是,在连接服务或接收广播时可能会创建片段。
我是否需要在片段和活动之间实现双向交互?
activity.updateList - >片段
fragment.getCurrentList - >活性
或者我应该在每个片段中注册一个新的braodcast接收器,这可能会导致大量冗余代码?
我知道我也可以使用onCreate Bundle传递数据。但是,我并不喜欢把所有这些东西都包括在内,我也相信它是非常缓慢和低效的。
答案 0 :(得分:0)
您可以使用本地广播管理器在多个片段和意图之间进行通信
//Send Broadcast
private void sendLocationBroadcast(Intent intent, Context context, String Message){
intent.putExtra("Message", Message);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
//In this snippet you point out your fragment or activity by given sample
if(Origin == Enum_Fragments.Task.getInteger())
{
intent = new Intent("Task");
sendLocationBroadcast(intent, context, "Task");
}
// What I have done here is made a enum for fragments and trigger service with related Identity (which was assigned by me to each fragment) after condition identifies the Argument (Origin) it will send data to desired framgment
这是ref
的枚举类public enum Enum_Fragments{
Task(1), Frag Notes(2), Default(0);
Enum_Fragments(int s) {
INTEGER_FOR_MODULE = s;
}
private int INTEGER_FOR_MODULE;
public int getInteger() {
return INTEGER_FOR_MODULE;
}
public void setInteger(int iNTEGER_FOR_MODULE) {
INTEGER_FOR_MODULE = iNTEGER_FOR_MODULE;
}
public static Enum_Fragments value(String i) {
try {
return valueOf(i);
} catch (Exception ex) {
return Default;
}
}
这将根据提供的意图发送您的广播以及完成工作后的某些特定数据,您可以在活动中收到它,如下所示
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String Message = intent.getStringExtra("Message");
//Todo
Log.d("Local BroadCast", "Recieved " + Message);
}
};