3

First of all let me say I hope everybody on the East Coast is okay after Hurricane Sandy. I was lucky in that, although I live in New York City, I never even lost power. My thoughts go out to those of you who weren't as fortunate. Now that I'm back to work after three days, I need some help with fragments. I'm trying to pass a Bundle to a Fragment and it's not working. I know I've got the general fragment setup correct, because if I don't try to pass a Bundle, it works fine. So here's my Activity where 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's my activity without the Bundle. This works fine:

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 info from the Bundle, it doesn't work. Here's 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 Activity, the app just hangs. LogCat doesn't seem to give me much info on this. Does anyone see any problem here?

Thanks!

Marc
  • 16,170
  • 20
  • 76
  • 119
Melanie
  • 3,021
  • 6
  • 38
  • 56

1 Answers1

5

You need to set the arguments on the Fragment.

In the Activity's onCreate:

Fragmet myFragment = new MyFragment();
myFragment.setArguments( getIntent().getExtras() );
Khantahr
  • 8,156
  • 4
  • 37
  • 60
  • Thanks, Ralgha. I implemented the two lines you suggested just below the super.OnCreate line in my Activity's onCreate method. I didn't change anything else - I'm assuming I still need to attach the Bundle to my Intent with putExtras in the onItemClick method, right? However, when I run this and make my choice in the Activity, my app just tells me "Unfortunately, MyProject has stopped" – Melanie Nov 01 '12 at 20:30
  • 1
    Also, I assume you meant: Fragment myFragment = new Fragment(); right? – Melanie Nov 01 '12 at 20:41
  • Also, I misrepresented the error I was getting. The app doesn't hang; it tells me "Unfortunately, MyProject has stopped." This is true whether I implement the lines of code Ralgha suggests or not. – Melanie Nov 01 '12 at 20:45
  • I also want to clarify - you mean to add this code to the Activity that contains my fragment, not the Activity that starts the activity that contains my fragment, correct? If that makes any sense. ;) – Melanie Nov 01 '12 at 21:18
  • Yes, added to the Activity that contains your fragment. Post the code where you create the Fragment please. – Khantahr Nov 01 '12 at 22:27
  • I've done that and the behavior is the same. I also tried adding it to the onCreate method of the fragment itself, but getIntent is undefined there. – Melanie Nov 02 '12 at 14:47
  • None of your posted code creates a fragment, I need to see the code in your Activity that's creating the fragment. – Khantahr Nov 02 '12 at 17:05
  • In the Activity which contains the Fragment, I have an onCreate method. It calls super.onCreate and then setContentView with the XML that defines my two fragments. After doing some research, I'm wondering if I also need an onCreateView method which returns a View, but I've tried to add that code and it doesn't help. If this isn't what you're looking for, let me know and thanks very much. – Melanie Nov 02 '12 at 17:37
  • Ah I didn't know you were doing them in xml. http://stackoverflow.com/questions/8641575/custom-attributes-in-android-fragments should help you out. – Khantahr Nov 02 '12 at 18:37