0

I try to send data from fragment to activity based on this reference. So i implement this code to send data to activity :

In MapFragment.java

    @Override //::LocationListener (Interface)
    public void onLocationChanged(Location location)
    {

        mCurrentLocation = location;
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()), 15);
        googleMap.animateCamera(cameraUpdate);

        double x = location.getLatitude();
        double y = location.getLongitude();

        String request = url+x+","+y;
        Log.d("log1=",request);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, request,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String _response) {

                        Log.d("log_res=",_response);
                        sendData(_response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                // Error handling
                Log.d("log_err=", error.toString());

            }
        });

        Volley.newRequestQueue(activity.getBaseContext()).add(stringRequest);

        Toast.makeText(activity, "Update location user ==>" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
        Log.d(TAG, "Current Location :" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude());
    }

    //===send data to activity===

    private void sendData(String _Output)
    {
        try {

            Log.d("output",_output);

            Intent intent = new Intent(getActivity().getBaseContext(), MainActivity.class);
            intent.putExtra("message", latd.toString() + "," + lang.toString());
            Log.d("msg=", latd.toString() + "," + lang.toString());
            activity.startActivity(intent);

        }
        catch (Exception jsoe){

            Log.e("err=>", jsoe.toString());
        }
    }

    //===interface===
    OnFragmentChangedListener mCallback;

    // Container Activity must implement this interface
    public interface OnFragmentChangedListener {
      public void onMapChanged(String name);
    }


    @Override //::BaseFragment (Class)
    public void onAttach(Activity _activity) {
      super.onAttach(_activity);
      this.activity = _activity;

      try {
         mCallback = (OnFragmentChangedListener) activity;
      } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString()
                  + " must implement OnHeadlineSelectedListener");
      }

    }

in MainAcitvity.java

 @Override
    public void onMapChanged(String name)  {
            Log.d("act=>", name);
            Intent intent = getIntent();
            String message = intent.getStringExtra("message");

    }

A Little explanation about above code : in fragment, everytime GPS detect new coordinat, i capture latitude and longitude, then i try to send to activity.

However, evertime i call activity.startActivity(intent); then this application reopen new Activity unlimitedly.

Community
  • 1
  • 1
questionasker
  • 2,536
  • 12
  • 55
  • 119
  • Is your MapFragment running on top of your MainActivity? – Jiyeh Jan 11 '16 at 06:38
  • @Jiyeh yes, right...it run with this command : `getSupportFragmentManager().beginTransaction().add(R.id.map_frame, new MapFragment()).commit();` – questionasker Jan 11 '16 at 06:40
  • Okay then I think the "unlimitedly opening" is due to this line of code: Intent intent = new Intent(getActivity().getBaseContext(), MainActivity.class).. Where you are making recursive call to your MainAcitivity. – Jiyeh Jan 11 '16 at 06:43
  • @Jiyeh right, but i need to send `latd` `lang` data to other fragment through mainactivity... – questionasker Jan 11 '16 at 06:45

1 Answers1

1

Add lunchMode to your activity at manifest.xml.

<activity
    android:name=".MainActivity"
    android:launchMode="singleTask">
</activity>
Rico Teng
  • 236
  • 1
  • 7
  • Hi, thanks. this fix the problem. but why i still not get the data that send from fragment ? – questionasker Jan 11 '16 at 06:49
  • Are you sure the 'ladt' object has data? Maybe you should paste your log info. – Rico Teng Jan 11 '16 at 06:55
  • i'm sure about it, in fragment the log show it, but in MainActivity i dont get it. maybe something wrong while implement interface. – questionasker Jan 11 '16 at 07:08
  • Your pasted code is right.But the onMapChanged method override from super class or interface,maybe there have something wrong.You can add breakpoint to debug it.Good lucky. – Rico Teng Jan 11 '16 at 07:17
  • Solved, Based on this [reference](http://inducesmile.com/android/android-fragment-how-to-pass-data-between-fragments-in-an-activity/) – questionasker Jan 11 '16 at 07:37