0

I have an activity, which is getting some string from other activity:

public class ShowPoints extends AppCompatActivity {

    public String points;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_points);

        Intent i = getIntent();
        points = i.getStringExtra("geoPoints");
    }
}

That works.

From this activity ShowPoints, I want to send the received string points to its fragment called PointsOnMap

For that, I created the fragment PointsOnMap and did this:

public class PointsOnMap extends Fragment implements OnMapReadyCallback {
      ......
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        act = new ShowPoints();
        String points = act.points;
        mView = inflater.inflate(R.layout.activity_points_on_map, container, false);
        return mView;
    }

But the String points of the fragment PointsOnMap its never filled with the string coming from the activity ShowPoints.

I am wondering what I am doing wrong. I have read some other posts here and seems like I am doing it in the right way.

----EDIT----

The last edits I did on the code seems not to work, I am pretty sure the problem is in the part where I am passing the parameters, since, without parameters passing, everything works and the initial map is showed.

so until now, the activity that contains the class is like this:

public class ShowPoints extends AppCompatActivity {

    public String points;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_points);

        Intent i = getIntent();
        points = i.getStringExtra("geoPoints");

        PointsOnMap pointsOnMapFragment = new PointsOnMap();
        Bundle bundle = new Bundle();
        bundle.putString("geoPoints",points);
        pointsOnMapFragment.setArguments(bundle);

        Log.d("REACHED HERE","---------");
    }
}

And the part of the fragment where i am retrieving the passed string is like this:

public class PointsOnMap extends Fragment implements OnMapReadyCallback {
      ......
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Bundle args = getArguments();
        geopoints = args.getString("geoPoints");
        mView = inflater.inflate(R.layout.activity_points_on_map, container, false);

        return mView;
    }

It is still not working for some reason.....The traceback indicates null object reference on the fragment on line geopoints = args.getString("geoPoints");

codeKiller
  • 5,493
  • 17
  • 60
  • 115

5 Answers5

1

If you want to pass info at the fragment creation you need to call setArgument, here an example https://stackoverflow.com/a/17436739/6726261

If you need to pass data after your fragment is created you can define a callback function inside your fragment to apply Android doc has a great example https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Community
  • 1
  • 1
Maurizio Ricci
  • 462
  • 1
  • 4
  • 11
1

I faced same Issue before, you might face many problems while passing intent data while still in the same thread, so i suggest to store your data into SharedPrefrence and then pass it with Intent like :

public String points;
Intent i = getIntent();
SharedPreferences settings = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("geoPoints", "geoPoints");
editor.commit();`
points = i.getStringExtra(getSharedPreferences("pref",MODE_PRIVATE).getString("geoPoints",""));
1

while you replace/add PointsOnMap fragment from ShowPoints activity:

PointsOnMap pointsOnMapFragment = new PointsOnMap();
    Bundle bundle = new Bundle();
    bundle.putString("geoPoints",geoPoints);
    pointsOnMapFragment.setArguments(bundle);

and get geoPoint on fragment use :

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String geoPoint = getArguments().getString("geoPoints");
}

For passing data from activity to activity we use intent object,and for passing data from activity to fragment OR fragment to fragment we use setArgument(Bundle bundle) method of Fragment to carry data.

Shrikant
  • 247
  • 1
  • 13
1

For sending data in fragment, you can choose two method

Method 1 Use Bundle and setArguments in Fragment:

Bundle bundle = new Bundle();
bundle.putString("data", "Val");
// set Arguments
FragmentClass frag = new FragmentClass();
frag.setArguments(bundle);

In Fragment onCreatView() call getArgument like getIntent in Activity

Bundle bundle = getArguments();

Method 2 Send data in constructor

Bundle bundle = new Bundle();
bundle.putString("data", "Val");
// set Arguments
user new FragmentClass().newInstance(bundle);

and in fragment class:

public static FragmentClass newInstance(Bundle bundle) {
    FragmentClass frag = new FragmentClass();
    frag.setArguments(bundle);

    return frag;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Bundle bundle = getArguments();    
    return inflater.inflate(R.layout.fragment, container, false);
}
Harsh Mittal
  • 449
  • 3
  • 10
  • yeah I have read on another post this Bundle method too, and for some reason it did not work....maybe I did something wrong....I will try again, thanks. – codeKiller May 10 '17 at 10:09
0

To pass data from Activity to Fragment

ShowPoints

Intent i = getIntent();  // receive from others activity
points = i.getStringExtra("geoPoints");

Bundle bundle = new Bundle(); // ready to pass to fragment 
bundle.putString("points", points);
// set Fragmentclass Arguments
PointsOnMap fragobj = new PointsOnMap();
fragobj.setArguments(bundle);

In PointsOnMap Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("points"); // receive from Activity  ShowPoints
    return inflater.inflate(R.layout.fragment, container, false);
}

Edit (PointOnMaps)

  public class PointsOnMap extends Fragment implements OnMapReadyCallback {
      ......
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View mView = inflater.inflate(R.layout.activity_points_on_map, container, false);

        Bundle bundle=this.getArguments();
        if(getArguments()!=null)
        {
           geopoints = bundle.getString("geoPoints");
        }

        return mView;
    } 
John Joe
  • 12,412
  • 16
  • 70
  • 135