问题
I am very struggling to solve this problem
I am listening data from conversation list fragment
Now how to send received data in chatting activity when chatting is going on? Please help.
回答1:
If I undestood the problem correctly, this seems like a job for an EventBus. Have a look on how to implement it here : http://square.github.io/otto/
First of all you need to define a event
public class ChatMessage{
private String status,message,sender;
//with constructors and toString
}
In your Chatting activity you post the event on the event bus, something like this
EventBus.getDefault().post(new ChatMessage(status,message,sender)
In your Conversation list (assuming you're displaying with a List or Recyclerview managed by an adapter) , make the adapter aware of your event bus
@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 a message to the EventBus at anytime you want the Conversation list fragment to be updated(eg. when a message is incoming , when you are sending a message to a contact)
If you can provide additional code form your fragments/activities and form the adapter I'll update my answer.
回答2:
The ChattingActivity is a separate Activity which requires to have a BroadcastReceiver to listen to the new chat messages received in ConversationListFragment.
The implementation of setting up a BroadcastReceiver is fairly simple. Take this as an example. The ChattingActivity will have these things in it.
@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();
}
Now the ConversationListFragment should send the broadcast accordingly when a new chat message is received using the same topic name. Let us assume there's a function in ConversationListFragment which is onNewMessageReceived. The parameter of the function which is newMsg which is the message received by the application in 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);
}
You might consider passing additional parameters as well. For example, the new message is received for other conversation, which is not opened right now in ChattingActivity. So the ChattingActivity should not be updated in that case. So we should send a flag, like otherPartyAccountId indicating which person I am chatting with. So if the ChattingActivity is opened with a person, but the message is received for another person, the current ChattingActivity should not be updated.
I would suggest to open the ChattingActivity as another Fragment under the HostingActivity. Which will make the whole process flow a lot simplified.
来源:https://stackoverflow.com/questions/48719795/how-to-transfer-data-between-fragment-and-non-parent-activity