getArguements() in Fragment not reading the strings sent from Activity

☆樱花仙子☆ 提交于 2020-06-17 10:01:08

问题


I am creating a chat app. So trying to send logged in username and one of the the user from userlist to a Fragment which receives these Strings and accordingly saves data as user_chatbuddy child in firebase. The data put in bundle does show same values on Toast when is use "bundle.toString()" on Toast. However the getArguements() in the sendMsgFragment is getting only null values.

Here are the relevant codes -

UsersList.java code -

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);
           startActivity(goChatActivity);

            String currentUser = "Abdul"; **//I am getting dynamic data here**
            String chatBuddy = "Razzak";  **//These are just dummy names for understanding**


            Bundle bundle = new Bundle();
            bundle.putString("currentUser", currentUser);
            bundle.putString("chatBuddy", chatBuddy);

            SendMsgFragment sendMsgFragment = new SendMsgFragment();
            sendMsgFragment.setArguments(bundle);
            Toast.makeText(getApplicationContext(), bundle.toString(), Toast.LENGTH_SHORT).show(); **// This toast shows bundle strings like bundle currentUser=Abdul, chatBuddy=Razzak**

ChatActivity.java code where i am replacing frame with SendMsgFragment looks like this -

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        loadFragment(new SendMsgFragment()); **//loading fragment here**
}

private void loadFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        fragmentTransaction.commit();

    }

SendMsgFragment.java code looks like below. I am using Onclick on image which when clicked will save data in firebase. But on testing it only puts null_null in database for logged in user and chatBuddy. So does the Toast at the end of this code -

    sendImgView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                currentUser = getArguments().getString("currentUser"); **// This returning null**
                chatBuddy = getArguments().getString("chatBuddy"); **// This also returning null**
            }catch (Exception ex){
                ex.printStackTrace();
            }
            Chat chat = new Chat(chatEditText.getText().toString(), cal.getTime().toString());

            Toast.makeText(getActivity(), currentUser + "_" + chatBuddy, Toast.LENGTH_LONG).show();
            chatEditText.setText("");
        }
    });

回答1:


In your first code snippet, the SendMsgFragment you are creating is different from the one you are creating in the second snippet:

SendMsgFragment sendMsgFragment = new SendMsgFragment();
sendMsgFragment.setArguments(bundle);
Toast.makeText(getApplicationContext(), bundle.toString(), Toast.LENGTH_SHORT).show();
loadFragment(new SendMsgFragment());

...

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();

Constructing the first fragment and setting its arguments doesn't actually do anything, because it's the second fragment that you show to the user.

You can pass the values to the activity in the first code block:

Intent goChatActivity = new Intent(UserList.this, ChatActivity.class);
goChatActivity.putExtra("currentUser", currentUser);
goChatActivity.putExtra("chatBuddy", chatBuddy);
startActivity(goChatActivity);

And then you can retrieve them and give them to the fragment in the second block:

String currentUser = getIntent().getStringExtra("currentUser");
String chatBuddy = getIntent().getStringExtra("chatBuddy");

Bundle bundle = new Bundle();
bundle.putString("currentUser", currentUser);
bundle.putString("chatBuddy", chatBuddy);

Fragment f = new SendMsgFragment();
f.setArguments(bundle);

loadFragment(f); **//loading fragment here**


来源:https://stackoverflow.com/questions/61549346/getarguements-in-fragment-not-reading-the-strings-sent-from-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!