How to transfer values ​​from activity to fragment without recreating it? Need to add an item:

date

public class DialogList { public String name; public String age; public int photoId; public DialogList(String name, String age, int photoId) { this.name = name; this.age = age; this.photoId = photoId; } } 

Fragment

 private List<DialogList> persons; public void addItems(String word) { persons.add(new DialogList(word, "35 years", R.drawable.ic_launcher_foreground)); rv.getAdapter().notifyDataSetChanged(); } 

Activity

 public void fragment(String agr){ MyFragment catFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.frame_container); catFragment.addItems(agr); } 

Here addItems is red

xml Activities

  <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/relativeLayout"> </FrameLayout> 

    1 answer 1

    Lights in red means it does not see such a method for an object from which you are trying to call it. getFragmentManager () may return a Fragment object to you, but it does not have such a method. Try to keep the link to MyFragment when you create it and call this method on it.

    • Thank you, did so 'com.app.Fragment.MyFragment' - Earned - Sertb