-3

How can I solve this, I can do this in Activities, but I can't do this in Fragment.

Will it causes any issues and why?

Cannot resolve method 'getIntent' in fragment

String name = getIntent().getStringExtra("name");
String profile = getIntent().getStringExtra("image");

Full code here:

public class ProfileFragment extends Fragment {

FragmentProfileBinding binding;

private TextView name;
private CircleImageView profile;
FirebaseDatabase database;
FirebaseStorage storage;


public ProfileFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    binding = FragmentProfileBinding.inflate(getLayoutInflater());
    View rootView = binding.getRoot();

    database = FirebaseDatabase.getInstance();
    storage = FirebaseStorage.getInstance();

    String name = getIntent().getStringExtra("name");
    String profile = getIntent().getStringExtra("image");

    binding.nameId1.setText(name);
    Glide.with(ProfileFragment.this).load(profile)
            .placeholder(R.drawable.avatar)
            .into(binding.profile);



    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

}`

Alex Mamo
  • 112,184
  • 14
  • 139
  • 167
KABIR
  • 3
  • 1
  • 4

1 Answers1

1

You are getting the following error:

Cannot resolve method 'getIntent' in fragment

Because getIntent() method is apart of the Activity class and not of a Fragment, hence the error. So in order to access the getIntent() method, you need to call the activity first. So the following line might do the trick:

String name = getActivity().getIntent().getStringExtra("name");

Alternatively, you can use a Bundle as explained in one of the answers from the following post:

Alex Mamo
  • 112,184
  • 14
  • 139
  • 167
  • When I'm using it, it doesn't show any data.. **String name = getActivity (). GetIntent (). GetStringExtra ("name");** – KABIR Aug 31 '21 at 08:34
  • How do you check that? Besides that, how do you add the data to Intent? Please edit your question and add the code that does that. – Alex Mamo Aug 31 '21 at 08:40
  • I'm showing the name and picture in the chat activity from the firebase database and the exact same data I want to see in the fragment which will be the profile fragment. – KABIR Aug 31 '21 at 09:00
  • Ok, what is the exact code that you are using now and doesn't work the way you expect? – Alex Mamo Aug 31 '21 at 09:07
  • Right, how can I fix this... – KABIR Aug 31 '21 at 09:12
  • My last comment is a question. Can you answer it? – Alex Mamo Aug 31 '21 at 09:13
  • Yes, I read your last comment, that's right. – KABIR Aug 31 '21 at 09:15
  • You read the comment but you didn't answer the question. – Alex Mamo Aug 31 '21 at 09:17
  • **I have used this code once in chat activity and it is working properly, I want to use it again in fragment to display the profile picture and name.** – KABIR Aug 31 '21 at 11:05
  • To properly use it in the Fragment, you need to call getActivity(). If you have added the method call, then be sure you have added the correct data. *I want to use it again in fragment to display the profile picture and name.* This is what the code from my answer does. – Alex Mamo Aug 31 '21 at 11:07