I have both A and B activities.
A uses GoogleMaps for adding markers, B to adding lines.
But A and B opens their own map.
How can i use same map on both activities? I don't want to pass marker and line datas again and again for every load.
You can use EventBus https://github.com/greenrobot/EventBus to pass any complex object around in Android.
Example passing the map from one Activity to another:
Add your GoogleMap on the EventBus:
EventBus.getDefault().postSticky(googleMap);
startActivity(new Intent(getActivity(), SomeActivity.class));
Anywhere in SomeActivity:
GoogleMap googleMap = EventBus.getDefault().getStickyEvent(GoogleMap.class);
This examle is just to show that it works across Activities, you can call getStickyEvent to get your map anywhere in your code.
Related
I have an Activity with a fragment, this fragment has a map in it and some markers.
I run a service in the background that changes the position of the markers.
Each Marker has a boolean "isDrawn" and whenever the boolean is set to false the map is updated by adding drawing the marker ( I use LiveData to observe the markers)
Whenever I close the Activity that contains the fragment I call onDestroyView, which sets isDrawn of every marker to false. That way when I open the Activity again, the markers get drawn one more time. All of this works fine.
The problem is this: in the Fragment I can tap on the markers, which opens a view that has a button which opens another Activity, when I close this Activity (With BackButton) and if a marker changed its position (through the service) when I was in said Activity, I find that there are two of the same marker on the map.
Any idea what I could do? Should I remove the fragment and create it again when I close the activity? is that possible? if so how should I proceed?
Here you can try 2 options
Try creating a static variable and and store some flag values.
check whether the Fragment is visible with the help of isVisible() or getUserVisibleHint()
You must be using this method to add a marker to the map.
public final Marker addMarker (MarkerOptions options);
take one variable of type Marker and store the reference of it which is returned by addMarker (MarkerOptions options);
like
if(marker!=null){
marker.remove(); //will clear the previous one, it might be null at first.
}
marker=addMarker(yourOptions);//will add the latest one
I have an activity which creates a new fragment containing SupportMapFragment, using FragmentTransaction. In this new fragment I am creating the google maps object in onMapReady method, and also retrieving the user location.
In the activity I will need to create a new fragment, that also contains a SupportMapFragment, but with different layout than the previous one. The flow will look like this:
Activity -> MapFragment1 -> get google maps and location
-> MapFragment2 -> get google maps and location
I will need something like this:
Activity -> get google maps and location -> MapFragment1
-> MapFragment2
Is there a way to actually create the google maps and have the user location in the activity, and then somehow pass it too all the fragments derived from the activity?
Thank you for your answers!
I have in my project an activity (that extends FragmentActivity), and two fragments (extends v4.fragment and one of them has a map fragment).
I made my project for tablet, and both fragments are shown on screen.
the problem is, I tried communicating between them (adding a marker on map from another fragment) , by using an Interface that is implemented in the activity.,
and for some reason my googleMap object returns null.
when I'm trying to use method inside the fragment
(e.g: options.mapType(GoogleMap.MAP_TYPE_SATELLITE)) using my googleMap object
it's working.
any idea what did i do wrong?
My bad. rookie mistake- I did'nt call the right fragment id from xml.... ( :
I have an application in which I am attaching different fragment with activity. and in that activity container I am replacing different fragment.
So at the start of the activity I replace the MapFragment in the activity container. Later on I replace other fragment and then from main activity I replace again the map fragment. Although I think I have wrong implementation of map in fragment but I wanted to know and wanted to do some different things and I think as I have no better understanding of fragment so I think that is why I am getting null pointer exception
What I have done SO far
Case 1.
AS I said On App start I am replacing map fragment but doing nothing more except showing user current location by just setting user location enable in google map v2. so on the button click of the map let say show other location , fetch location information from server and draw pins on the map. this is working good as far as the map fragment is there.
Case2
if user click button let say "Show location" while there is other fragment in the container let say About Us fragment which contains app info , so now on this button it is supposed that map fragment should be replaced again and then the function in that fragment which is going to download locations from the server and to show them on map should start working as I am calling on button click
fragmentTransaction1.replace(R.id.frame, myMapFragment, "MyMapFragment").commit();
fragmentManager.executePendingTransactions();
myMapFragment.GetAllLocations("friendsLocation");
so this GetLocations() should run , but at this stage the app crash saying null pointer exception. and in this function myMapFragment.GetAllLocations("friendsLocation")
there is a need of context which I taking as getActivity(), the context is needed as I have to make a call to web api using ION library which needs context.
So these are my basic questions
How can I get to know that fragment is started and then the function which I have called from MainActivity should run after the mapFragment is loaded and started so that I can not get the null context in the getActivity() ?
Is there any other way of doing that , I hope you understand my need that is , I want to replace the map fragment and I want to call the fragment method on the same time by passing the parameters to fragment method from the main activity , but How can it be done ?
An idea is in my mind and that is while replacing back the map fragment through Mainactivity button , i should set some static value in the mapFragment let say
public static boolean isLocationShouldDownload== flase //in map
fragment
I should set it true while replacing fragment
and then when the fragment is started after it is in running condition it should check this variable if it is true (set by the main activity) then it should download and start plotting pins. but I have no idea how can I get to know that fragment is running .
I have no idea how and where I have to implement my idea which I have stated in point no 3.
Please share your idea with me and give me suggestion or share me a source code that how can I achieve this task through main activity that fragment should replace and download the locations and to show them on map without getting app crash.
EDIT
I've found a better STR:
Make sure to set "Do not keep activities" in Developer options in Settings.
Open the app with a SupportMapFragment as a child fragment of another fragment.
Switch to another app
Open your app again
Notice you can't interact with the map and no animations work.
Open another screen within the app
Notice there's a single frame or so of the map with the markers drawn on screen.
I have an issue with Google Maps API v2.
I am animating the camera to zoom to a set of custom marker bitmaps rendered on a MapFragment.
On selecting one of these marker tooltips I open a geo: intent (for the Google Maps app etc.)
When the user presses back it reopens my activity with the fragment back stack rebuilt.
My issue is that it doesn't render camera animations or the markers on coming back, though there is a brief display of those markers when the user presses back to go to the previous fragment.
The GoogleMap instance has the markers, but it doesn't render them - I'm guessing because the MapFragment/MapView thinks it doesn't need to be rendered.
How do I force a render of the MapView? Failing that, how do I get the MapView to recognise that the model has changed?
The issue was to do with the way I was handling my child fragments.
Every time the parent fragment would call onCreate the children fragments would get recreated.
I did the following to handle my child fragments, but there may be a better way:
private static final String TAG_FRAGMENT_MAP = "TagFragmentMap";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
if (savedInstanceState == null) {
// create the fragments for the first time
ft.add(R.id.view_flip, new SupportMapFragment(), TAG_FRAGMENT_MAP);
ft.commit();
}
}
// ...
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
mMapFragment = (SupportMapFragment)findFragmentByTag(TAG_FRAGMENT_MAP);
}