On some devices map is right, but on the some devices gray screen shows instead the map
FragmentManager fm = getFragmentManager();
mapFragment = (MapFragment) fm.findFragmentById(R.id.map_container);
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
Log.d("CHECK_SPEED", "replace");
fm.beginTransaction().replace(R.id.map_container, mapFragment)
.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out)
.commit();
setupGoogleMap();
}
Related
I'm facing problems when placing SupportMapFragment inside a fragment that is part of a viewpager with 5 tabs, everything works correctly when viewpager loads the map for first time, but not showing anything whe comming back from another tab. I load the map as follows:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getFragmentManager();
mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_estate);
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map_estate, mapFragment).commit();
}
mapFragment.getMapAsync(this);
}
and then use it like:
#Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
this.map.setMyLocationEnabled(true);
this.map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
this.map.getUiSettings().setMyLocationButtonEnabled(false);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(center, 16);
mapFragment.getMap().moveCamera(cameraUpdate);
}
Anyone facing same problem? Thanks in advance
ReplacingFragmentManager fm = getFragmentManager(); for FragmentManager fm = getChildFragmentManager(); solved the problem.
I am new to android working first time on fragment.I am creating an app in which I want to add a fragment on frame layout.I am able to do this but now what I want is to remove the same fragment which i added by that same button click I tried but couldn't. here is my code.
public void onClick(View v) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
if(v.getId() == R.id.clickme){
if(getSupportFragmentManager().findFragmentById(R.layout.fragment_one) != null){
// getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.layout.fragment_one)).commit();
Fragment fragment = new FragmenOne();
fragmentManager.beginTransaction().remove(fragment).commit();
}else{
Fragment fragment = new FragmenOne();
// android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.my_frame, fragment)
.commit();
}
}
}
You need to do in this manner
Fragment f = fragmentManager.findFragmentById(R.id.my_frame);
if(f instanceof FragmenOne) {
FragmenOne oneFragment = (FragmenOne) f;
FragmentTransaction trans = manager.beginTransaction();
trans.remove(oneFragment);
trans.commit();
fragmentManager.popBackStack();
}else{
Fragment fragment = new FragmenOne();
fragmentManager.beginTransaction()
.replace(R.id.my_frame, fragment)
.commit();
}
So i have an app that uses a MapFragent in two different activities. The first one is implemented in the xml layout of that activity but as many of you are probably aware, the only way it seems to implement another map fragment in a different activity is to do it in java like so:
mMapFragment = new DRPCustomMapFragment();// this is just a class extending SupportMapFragment
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.mapContainer, mMapFragment);
fragmentTransaction.commit();
fm.executePendingTransactions();
//
so in onCreate() i am doing exactly that. the problem is if i try to reference mMapFragment immediately after that, its still null. so i ended up having to add this ugly block of code:
mMapFragment = new DRPCustomMapFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.mapContainer, mMapFragment);
fragmentTransaction.commit();
fm.executePendingTransactions();
//
_mapContainer.post(new Runnable() {
#Override
public void run() {
_map = mMapFragment.getMap();
mMapFragment
.setOnDragListener(new DRPMapWrapperLayout.OnDragListener() {
#Override
public void onDrag(MotionEvent motionEvent) {
Log.d("ON_DRAG",
String.format("ME: %s",
motionEvent.getAction()));
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
_scrollView
.requestDisallowInterceptTouchEvent(true);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP
|| motionEvent.getAction() == MotionEvent.ACTION_OUTSIDE) {
_scrollView.requestDisallowInterceptTouchEvent(false);
_locationForDrop = _map.getCameraPosition().target; }
}
});
setUpMapUI();
}
});
please tell me there is a better way.. pretty please?
Im developing an android Tab app using FragmentTabs, A Child Fragment class uses Google Maps V2,
map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
Once i clicked on a map marker, i need to move to another Child Fragment Class,
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.container_framelayout, new FragmentClass());
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
});
But when i clicked on the marker it freeze my app, i couldn't find any exceptions either..
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
YourActivity activity = new YourActivity();
ft.addToBackStack(null);
ft.replace(R.id.container_framelayout, activity);
ft.commit();
}
});
Why is this returning the "fragment already added" error and crashing the app?
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_container, tempmainfrag);
ft.commit();
for(int i = 0; i < fm.getBackStackEntryCount(); i++)
{
fm.popBackStack();
}
I'm guessing it is because of the for loop as it works without it, but I am needing to clear the back stack, how can I properly do this?
I've had the same error when working with mapfragments, this is what finally solved it for me:
#Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment f = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.current_fragment);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
Hope it helps.