1

in my fragment the bundle is always null

main activity

public class MainActivity extends AppCompatActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle args=new Bundle();
        args.putString("itemname", "hello");
        beaconListFragment=new BeaconListFragment();
        beaconListFragment.setArguments(args);

        //i don't know if this is needed or not, i tried with and without it
        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, beaconListFragment,"tag").commit();
    }

    ...
    ..
    .

BeaconListFragment

public class BeaconListFragment extends android.support.v4.app.Fragment {
@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();


        //bundle is always null
        if (bundle != null) {
            String link = bundle.getString("itemname");

        }
    }
    ...
    ..
    .

most examples seem like this but i can't understand where is the problem

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pinale
  • 2,060
  • 6
  • 38
  • 72

3 Answers3

0

You should read the data in onCreate not onActivityCreated

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    if (getArguments() != null)
    {
        String link = bundle.getString("itemname");
    }

}
Mina Farid
  • 5,041
  • 4
  • 39
  • 46
0

For fragment there is a method like setArguments() in that method you can pass bundle object.

And in the fragment class you can access that bundle by getArguments().

In above way you can pass params

0

Check your import statements in the Fragment. Looks like you are mixing 2 types: one from SDK and another from support library. Make sure to use any one.

Your Fragment inherits from android.support.v4.app.Fragment. Your import statements are not visible in the code here. If your import statement has android.app.Fragment, then remove that line and try.

Bob
  • 13,447
  • 7
  • 35
  • 45