问题
This question is an extension to previous question over here. First Question
So the logged in user and chatbuddy names are not returning null any more and data is being inserted in firebase as well. However the setOnClickListener action takes back to previous UsersList.java activity even though no such Intent given in sendImgView.setOnClickListener() function.
UserList.java relevant code takes to ChatActivity as you can see and also i finish() it on click-
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// TODO Auto-generated method stub
Intent goChatActivity = new Intent(UserList.this, ChatActivity.class);
String currentUser = "Abdul";
String chatBuddy = "Razzak";
goChatActivity.putExtra("currentUser", currentUser);
goChatActivity.putExtra("chatBuddy", chatBuddy);
startActivity(goChatActivity);
finish(); **//finishing UsersList here**
}
});
And in the SendMsgFragment.java after inserting data in firebase I do getActivity().finish(); but sendImgView.setOnClickListener action taking back to UsersList.java from where I came to SendMsgFragment using Intent. How do I prevent that from happening -
SendMsgFragment.java relevant code is -
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.send_msg_fragment, container, false);
sendImgView = (ImageView) view.findViewById(R.id.sendButton);
sendImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
currentUser = getArguments().getString("currentUser");
chatBuddy = getArguments().getString("chatBuddy");
}catch (Exception ex){
ex.printStackTrace();
}
Chat chat = new Chat(chatEditText.getText().toString(), cal.getTime().toString());
databaseReference.child(currentUser+"/"+currentUser+"_"+chatBuddy).push().setValue(chat);
chatEditText.setText("");
getActivity().finish(); //finishing ChatActivity where I load this fragment
}
});
return view;
}
来源:https://stackoverflow.com/questions/61557077/why-does-click-event-in-fragment-takes-back-to-the-last-activity-without-any-suc