Reinitialising Fragment when closing activity - android

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

Related

How to move marker on tap instead of long press

What I want ?
When user taps on marker its image should change and user should be able to move the marker and I should be able to get the latitude and longitude . Below is a video of requirement .
What I have tried so far ?
I tried to implement default marker drag listener but it only works with long press .
I looked on similar questions on stack overflow but no luck .
Tried with ground overlay but not had much luck .
So how can I achieve this behaviour , Any hint is appreciated . There is already an app out there which does it so this should be possible .
As per documentation you are required to use:
GoogleMap.OnMarkerClickListener
public static interface GoogleMap.OnMarkerClickListener
Which Defines signatures for methods that are called when a marker is clicked or tapped.
With public method:
abstract boolean onMarkerClick(Marker marker)
Called when a marker has been clicked or tapped.
INSTEAD OF
GoogleMap.OnMapLongClickListener
public static interface GoogleMap.OnMapLongClickListener
with Public method:
public abstract void onMapLongClick (LatLng point)
Called when the user makes a long-press gesture on the map, but only if none of the overlays of the map handled the gesture. Implementations of this method are always invoked on the Android UI thread.

How to auto start function Fragment Life cycle

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.

Sharing GoogleMaps object between two activities

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.

Android Google maps how to zoom to a specific area

I have 2 different activities: On the first one i have my map and I am doing something else on the second one. I have a button on the second activity and I want that button to display a specific area on the map when it's clicked. But my code stops the app when I click the button. The weird thing is that if I remove the 2 lines of code above cameraUpdate, when the button is clicked, it just takes me to the other acitivity displaying the map, but I want it to zoom on a specific area, which it doesn't.
public void show_map(View x){
Intent intent = new Intent(getApplicationContext(), THEMAP.class);
startActivity(intent);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_AMERICA,13);
gMap.animateCamera(update);
}
You should be placing your code to manipulate the map in the Activity that actually contains the map, not in the launching Activity. You'd need to post more of your code for me to be sure, but try moving
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_AMERICA,13);
gMap.animateCamera(update);
into your onCreate() in THEMAP (not your actual class name I hope.

Android: Google Maps API v2 rendering issue with markers and camera animation

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);
}

Categories

Resources