How to pass a packet to a fragment

First of all, let me say that everyone on the East Coast is fine after Hurricane Sandy. I was lucky in this, although I live in New York, I have not even lost power. My thoughts go to those of you who are not so lucky. Now, when I get back to work in three days, I need help with fragments. I am trying to pass a bundle to a fragment and it does not work. I know that I set up the generic fragment correctly, because if I don't try to pass the Bundle, it works fine. So here is my activity in which I pass the Bundle:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent choice = new Intent(getApplicationContext(), com.MyProject.project.MyList.class); Bundle dataBundle = new Bundle(); String chosenValue = values[position]; dataBundle.putString("Level",chosenValue); choice.putExtras(dataBundle); startActivity(choice); } 

Now here is my activity without the Sachet. This works great:

 public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] values = new String[] { "Enterprise", "Star Trek", "Next Generation", "Deep Space 9", "Voyager"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } 

However, when I try to get information from the Bundle, this will not work. Here is my code:

 public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle info = getArguments(); String level = info.getString("level"); String[] values = new String[] {level, level}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } 

When I run this and click on my choice in my work, the application just freezes. LogCat doesn't seem to give me much information about this. Anyone see any problems here?

Thanks!

+4
source share
1 answer

You need to set the arguments in the fragment.

In onCreate action:

 Fragmet myFragment = new MyFragment(); myFragment.setArguments( getIntent().getExtras() ); 
+5
source

Source: https://habr.com/ru/post/1443524/


All Articles