Android - 活动重新打开由于在片段上发送数据而导致的无限制

时间:2016-01-11 06:25:03

标签: java android android-fragments android-activity fragment

我尝试根据this reference将数据从片段发送到活动。所以我实现此代码以将数据发送到活动:

MapFragment.java

//Parent layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

//Child layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
//content
</LinearLayout>

//Child layout 2
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

//content
</LinearLayout>

</LinearLayout>
MainAcitvity.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");
      }

    }

关于上述代码的一点解释: 片段,每当GPS检测到新的协调,我捕获纬度和经度,然后我尝试发送到活动

然而,我称之为 @Override public void onMapChanged(String name) { Log.d("act=>", name); Intent intent = getIntent(); String message = intent.getStringExtra("message"); } ;然后这个应用程序无限地重新打开新的Activity。

1 个答案:

答案 0 :(得分:1)

将lunchMode添加到manifest.xml的活动中。

if (yes.isChecked()) {
age =age + 15
                    editText3.setText(Integer.toString(age));
                } else if (no.isChecked()) {
                   age =age - 10; 
                    editText3.setText(Integer.toString(age));

               }