如何在片段和非父activity之间传输数据?
How to transfer data between fragment and non parent activity?
我很纠结这个问题
我正在收听对话列表片段中的数据
现在如何在聊天中发送收到的数据activity在聊天的时候?请帮忙
如果我对问题的理解正确,这似乎是 EventBus 的工作。在这里查看如何实现它:http://square.github.io/otto/
首先你需要定义一个事件
public class ChatMessage{
private String status,message,sender;
//with constructors and toString
}
在你的聊天中 activity 你 post 事件总线上的事件,像这样
EventBus.getDefault().post(new ChatMessage(status,message,sender)
在您的对话列表中(假设您显示的是一个由适配器管理的列表或 Recyclerview),让适配器知道您的事件总线
@Subscribe(threadMode = ThreadMode.MAIN)
public void updateAdapter(ChatMessage message) {
//here you should get the chat item and update it
//do not forget to call notifyDatasetChanged() at the end to update your adapter
}
Post 在您希望更新对话列表片段的任何时候向 EventBus 发送消息(例如,当消息传入时,当您向联系人发送消息时)
如果您可以从 fragments/activities 和适配器中提供额外的代码,我会更新我的答案。
ChattingActivity
是一个单独的 Activity
,需要 BroadcastReceiver
才能收听 ConversationListFragment
中收到的新聊天消息。
设置 BroadcastReceiver
的实现相当简单。以此为例。 ChattingActivity
里面会有这些东西。
@Override
public void onCreate(Bundle savedInstanceState) {
// Other code ...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "new-chat-message".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("new-chat-message"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "new-chat-message" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "New chat message received: " + message);
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
现在,当收到使用相同主题名称的新聊天消息时,ConversationListFragment
应该相应地发送广播。让我们假设 ConversationListFragment
中有一个函数 onNewMessageReceived
。函数的参数是 newMsg
是应用程序在 ConversationListFragment
中收到的消息。
// Send an Intent with an action named "new-chat-message". The Intent sent should
// be received by the ChattingActivity.
private void onNewMessageReceived(String newMsg) {
Log.d("sender", "Broadcasting new chat message");
Intent intent = new Intent("new-chat-message");
// You can also include some extra data.
intent.putExtra("message", newMsg);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
您也可以考虑传递额外的参数。例如,收到其他会话的新消息,该会话目前未在 ChattingActivity
中打开。因此在这种情况下不应更新 ChattingActivity
。所以我们应该发送一个标志,比如 otherPartyAccountId
表明我正在和哪个人聊天。所以如果 ChattingActivity
是用一个人打开的,但是消息是为另一个人接收的,那么当前的 ChattingActivity
不应该被更新。
我建议打开 ChattingActivity
作为 HostingActivity
下的另一个 Fragment
。这将使整个流程简化很多。
我很纠结这个问题
我正在收听对话列表片段中的数据
现在如何在聊天中发送收到的数据activity在聊天的时候?请帮忙
如果我对问题的理解正确,这似乎是 EventBus 的工作。在这里查看如何实现它:http://square.github.io/otto/
首先你需要定义一个事件
public class ChatMessage{
private String status,message,sender;
//with constructors and toString
}
在你的聊天中 activity 你 post 事件总线上的事件,像这样
EventBus.getDefault().post(new ChatMessage(status,message,sender)
在您的对话列表中(假设您显示的是一个由适配器管理的列表或 Recyclerview),让适配器知道您的事件总线
@Subscribe(threadMode = ThreadMode.MAIN)
public void updateAdapter(ChatMessage message) {
//here you should get the chat item and update it
//do not forget to call notifyDatasetChanged() at the end to update your adapter
}
Post 在您希望更新对话列表片段的任何时候向 EventBus 发送消息(例如,当消息传入时,当您向联系人发送消息时)
如果您可以从 fragments/activities 和适配器中提供额外的代码,我会更新我的答案。
ChattingActivity
是一个单独的 Activity
,需要 BroadcastReceiver
才能收听 ConversationListFragment
中收到的新聊天消息。
设置 BroadcastReceiver
的实现相当简单。以此为例。 ChattingActivity
里面会有这些东西。
@Override
public void onCreate(Bundle savedInstanceState) {
// Other code ...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "new-chat-message".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("new-chat-message"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "new-chat-message" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "New chat message received: " + message);
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
现在,当收到使用相同主题名称的新聊天消息时,ConversationListFragment
应该相应地发送广播。让我们假设 ConversationListFragment
中有一个函数 onNewMessageReceived
。函数的参数是 newMsg
是应用程序在 ConversationListFragment
中收到的消息。
// Send an Intent with an action named "new-chat-message". The Intent sent should
// be received by the ChattingActivity.
private void onNewMessageReceived(String newMsg) {
Log.d("sender", "Broadcasting new chat message");
Intent intent = new Intent("new-chat-message");
// You can also include some extra data.
intent.putExtra("message", newMsg);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
您也可以考虑传递额外的参数。例如,收到其他会话的新消息,该会话目前未在 ChattingActivity
中打开。因此在这种情况下不应更新 ChattingActivity
。所以我们应该发送一个标志,比如 otherPartyAccountId
表明我正在和哪个人聊天。所以如果 ChattingActivity
是用一个人打开的,但是消息是为另一个人接收的,那么当前的 ChattingActivity
不应该被更新。
我建议打开 ChattingActivity
作为 HostingActivity
下的另一个 Fragment
。这将使整个流程简化很多。