This question has already been answered:

It is necessary to transfer the text from the activity to the fragment.
Under this call method

public void NewsClick(View view) { getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, UniversalListFragment.newInstance("notes.json")).commit(); } 

did so, but nothing happens.
Here is the fragment code

  private static final String TAG_FRAGMENT = "notes.json"; public static UniversalListFragment newInstance(String name) { UniversalListFragment fragment = new UniversalListFragment(); Bundle args = new Bundle(); args.putString(TAG_FRAGMENT, name); fragment.setArguments(args); return fragment; } 

Explain how to transfer using this method. How to transfer a value from an activation to a fragment , instead of fragment.setArguments (bundle); write UniversalListFragment.setArguments (bundle); . And leave NewsClick as it is? and Bundle bundle = new Bundle (); in oncreate shove

Fragment code

 public class UniversalListFragment extends Fragment { private static final String NOTE_TITLE = "title" ; private static final String NOTE_CONTENT = "content" ; private JSONAdapter noteAdapter; private RecyclerView recyclerView; private LinearLayoutManager mLLM; private JSON note; private JSONObject jsonObject; private JSONArray jsonArray; private static final String TAG_FRAGMENT = "notes.json"; public static UniversalListFragment newInstance(String name) { UniversalListFragment fragment = new UniversalListFragment(); Bundle args = new Bundle(); args.putString(TAG_FRAGMENT, name); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View rv = inflater.inflate( R.layout.fragment_list, container, false); //recyclerview/// noteAdapter = new JSONAdapter(); recyclerView = (RecyclerView) rv.findViewById(R.id.recyclerview); recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); mLLM = new LinearLayoutManager(recyclerView.getContext()); recyclerView.setLayoutManager(mLLM); recyclerView.setAdapter(noteAdapter); try { loadNotes(); } catch (JSONException e) { e.printStackTrace(); } AppCompatActivity activity = (AppCompatActivity) getActivity(); ActionBar actionBar = activity.getSupportActionBar(); if(actionBar!= null) { actionBar.setTitle(R.string.start_service); } return rv; } private void loadNotes() throws JSONException { jsonArray = new JSONArray(getJSONString()); for(int i=0; i < jsonArray.length(); i++){ jsonObject = jsonArray.getJSONObject(i); note = new JSON(jsonObject.getString(NOTE_TITLE), jsonObject.getString(NOTE_CONTENT)); noteAdapter.addNote(note); } noteAdapter.notifyDataSetChanged(); } private String getJSONString(){ String json = null; try { InputStream is = getContext().getAssets().open(TAG_FRAGMENT); // go to main/asset directory to see notes.json int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } 

}

Reported as a duplicate by members pavlofff , cheops , Streletz , sercxjo , D-side 23 May '16 at 12:14 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Nothing this fragment is shown or the text does not come into it? - Yuriy SPb
  • one
    Get the arguments in the fragment constructor: String txt = getArguments().getString(TAG_FRAGMENT); - Jarvis_J
  • @AbrogPetrovich, can you cast a comment in the answers? - zayn1991
  • @AbrogPetrovich there are no arguments in the fragment constructor yet - temq
  • one
    @joni String strFromArgs = getArguments().getString("notes.json", "defaultValue"); - Yuriy SPb

2 answers 2

If it is necessary to display different data in a fragment depending on the parameters specified, the fragment code should be as follows:

  private static final String NOTE_TITLE = "title" ; private static final String NOTE_CONTENT = "content" ; private final String TAG_FRAGMENT = "tag_fragment"; private JSONAdapter noteAdapter; private RecyclerView recyclerView; private LinearLayoutManager mLLM; private JSON note; private JSONObject jsonObject; private JSONArray jsonArray; private String mJson; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View rv = inflater.inflate( R.layout.fragment_list, container, false); //recyclerview/// noteAdapter = new JSONAdapter(); recyclerView = (RecyclerView) rv.findViewById(R.id.recyclerview); recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); mLLM = new LinearLayoutManager(recyclerView.getContext()); recyclerView.setLayoutManager(mLLM); recyclerView.setAdapter(noteAdapter); try { loadNotes(); } catch (JSONException e) { e.printStackTrace(); } AppCompatActivity activity = (AppCompatActivity) getActivity(); ActionBar actionBar = activity.getSupportActionBar(); if(actionBar!= null) { actionBar.setTitle(R.string.start_service); } return rv; } private void loadNotes() throws JSONException { jsonArray = new JSONArray(getJSONString()); for(int i=0; i < jsonArray.length(); i++){ jsonObject = jsonArray.getJSONObject(i); note = new JSON(jsonObject.getString(NOTE_TITLE), jsonObject.getString(NOTE_CONTENT)); noteAdapter.addNote(note); } noteAdapter.notifyDataSetChanged(); } private String getJSONString(){ if(getArguments() != null){ mJson = getArguments().getString(TAG_FRAGMENT); } if(mJson == null || mJson.isEmpty() || mJson.equals("")) return null; String json = null; try { InputStream is = getContext().getAssets().open(mJson); // go to main/asset directory to see notes.json int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } } 

fragment call code:

 public void NewsClick(View view) { Bundle mArg = new Bundle(); mArg.putString("tag_fragment", youData); Fragment mFrg = new UniversalListFragment(); mFrg.setArguments(mArg); getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, mFrg).commit(); } 

where youData is the data to be received by the fragment;

  • Yes, I have already tried a lot of things. I found an error in InputStream is = getContext (). GetAssets (). Open () // it opens the first file for any value - djo
  • You do not know how to fix? - djo
  • So in your version of the fragment it will always read the same file InputStream is = getContext (). GetAssets (). Open (TAG_FRAGMENT) since TAG_Fragment will always be equal to "notes.json". In the variant proposed by me, you will pass the argument to the fragment with the name of the required file from the assets and then consider it. - mg-demin
  • @ m-gdemin, what's up setString is red - djo
  • or is there putString? - djo

You pass the variable directly to the function of receiving the fragment instance, but not where you use it.

In order to get your data with such a call system, use the code:

 public YouFragment extends Fragment{ public static YouFragment mInstance; private String mYouText; public static YouFtagment getInstance(String youText){ if(mInstance == null){ mInstance = new YouFragment(); } mInstance.mYouText = youText; return mInstance; } 

The data you send will be available in the mYouString variable.

  • With this code, you immediately get a leak, because the garbage collector will not be able to clear the memory to be allocated for the fragment. - temq
  • Explain how to transfer to this method ru.stackoverflow.com/questions/455014 , you just need instead of fragment.setArguments (bundle); write UniversalListFragment.setArguments (bundle); . And leave NewsClick as it is? and Bundle bundle = new Bundle (); in oncreate shove - djo
  • On the issue of memory leakage - the fragment will hang in FragmentManager in any case and until the activity is alive the garbage collector will not touch it. For greater loyalty, you can add an instance reset to onDestroy (). - mg-demin
  • and two fragments with different youText can not be created? viewPager, there ... or, for example, to create the same fragment from another activity, and then return to the previous one ... well, terrible. - Yura Ivanov
  • Judging by the presented code of the fragment, it will always output the same text from the hard-coded file in Assets. If such a functionality is needed, then in creating an instance on a fragment there is no need at all what is enough to simply hang up a new UniversalListFragment instead of UniversalListFragment.newInstance (..) - mg-demin