-2

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
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

2

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
  • 130,605
  • 17
  • 163
  • 193