我有一个应用程序,在一个活动中包含几个片段。 并且其中一个片段是用编辑文本实现的地图,用于基于相机更改监听器的用户地址自动填充,而我从该地图片段中按下我设法回到上一个片段,但是一旦我点击该地图片段中的编辑文本后退按钮关闭整个应用程序而不强制关闭。我不知道确切的问题。对不起,我的英语不好。
public class Maps extends Fragment implements OnMapReadyCallback {
Unbinder unbinder;
@BindView(R.id.address_submit)
Button addressSubmit;
private GoogleMap mMap;
IDU idu;
User user;
EditText addressDoor, addressAddressline, addressStreet, addressCity, addressPincode, addressState;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps, container, false);
unbinder = ButterKnife.bind(this, rootView);
Activity_Dashboard.navigation.hideBottomNavigation();
idu = new IDU_Presenter(this, getActivity());
user = SharedPreferenceManager.getInstance(getActivity()).getUser();
addressDoor = (EditText)rootView.findViewById(R.id.address_door);
addressAddressline = (EditText)rootView.findViewById(R.id.address_addressline);
addressStreet = (EditText)rootView.findViewById(R.id.address_street);
addressCity = (EditText)rootView.findViewById(R.id.address_city);
addressState = (EditText)rootView.findViewById(R.id.address_state);
addressPincode = (EditText)rootView.findViewById(R.id.address_pincode);
final RippleBackground rippleBackground = (RippleBackground) rootView.findViewById(R.id.content);
ImageView b = (ImageView) rootView.findViewById(R.id.confirm_address_map_custom_marker);
rippleBackground.startRippleAnimation();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// Log.i(tag, "keyCode: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
Fragment_Address_list fragment = new Fragment_Address_list();
FragmentActivity activitys = (FragmentActivity) getActivity();
FragmentManager fm = activitys.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_layout, fragment);
ft.commit();
return true;
}
return false;
}
});
return rootView;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
final Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
double lat = mMap.getCameraPosition().target.latitude;
double lng = mMap.getCameraPosition().target.longitude;
Location startPoint = new Location("locationA");
startPoint.setLatitude(11.010519);
startPoint.setLongitude(76.986481);
Location startPoint1 = new Location("locationb");
startPoint1.setLatitude(lat);
startPoint1.setLongitude(lng);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
if (addresses.size() == 0) {
Toast.makeText(getActivity(), "Invalid location", Toast.LENGTH_SHORT).show();
} else {
if (addresses.get(0).getThoroughfare() == null) {
addressAddressline.setText(addresses.get(0).getSubLocality());
} else {
addressAddressline.setText(addresses.get(0).getThoroughfare());
}
addressStreet.setText(addresses.get(0).getSubLocality());
addressCity.setText(addresses.get(0).getLocality());
addressPincode.setText(addresses.get(0).getPostalCode());
addressState.setText(addresses.get(0).getAdminArea());
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
LatLng TutorialsPoint = new LatLng(11.010519, 76.986481);
MarkerOptions markerOptions = new MarkerOptions().position(TutorialsPoint).title("Your location");
Marker mMarker = mMap.addMarker(markerOptions);
mMarker.showInfoWindow();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(TutorialsPoint, 18f));
}
您可以覆盖用户单击后退按钮时触发的onBackPressed()
方法。
@Override
public void onBackPressed() {
//DO YOUR STUFF LIKE SHOWING DIALOG
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
您可以在这里更改片段以返回到前一个片段(这是您使用fragmentManager时代码的默认行为),您可以显示Toast或其他任何内容。
希望能帮助到你。