java Android活动因在碎片上发送数据而无限重新打开
我试图基于this reference将数据从片段发送到活动。因此,我实现了以下代码,将数据发送到活动:
在地图片段中。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");
}
}
在维护中。java
@Override
public void onMapChanged(String name) {
Log.d("act=>", name);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
}
关于上述代码的一点解释:在片段中,每次GPS检测到新的坐标,我捕获纬度和经度,然后尝试发送到活动
然而,每次我打电话给activity.startActivity(intent)
;然后,该应用程序无限制地重新打开新活动
# 1 楼答案
在清单中将午餐模式添加到您的活动中。xml