0

I want to receive data from an Activity to a Fragment. In an Activity is easy:

Bundle b = getIntent().getExtras();
String flag = b.getString("flag");

Ok, for me, doesn't work

String flag = getIntent().getExtras().getString("flag");

Neither

String flag = getActivity().getIntent().getExtras().getString("flag");

Or.

Bundle b = getIntent().getExtras();

The error: "Unreachable code"

Any idea?

canova
  • 3,965
  • 2
  • 22
  • 39
ivan arias
  • 39
  • 6
  • 2
    you may have an if else chain that causes that piece of code to never run. Put a breakpoint on it and check it. – joao2fast4u Mar 25 '14 at 11:51

1 Answers1

2

You can view that on Fragment documentation.

Create a newInstance funcion in your fragment to initialize the fragment and add data with setArguments, passing a Bundle.

public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);

    return f;
}

Then get the arguments using:

 Bundle args = getArguments();
 int index = args.getInt("index", 0);
dcastro
  • 66,540
  • 21
  • 145
  • 155
José Barbosa
  • 913
  • 8
  • 17