Android Google Maps transition from portrait to landscape and vice versa - android

My application only displays the map on Google maps. My problem is when I change the position of my screen (the application I installed on my tablet), turning it there's a white screen that appears before displaying the map.
My configuration; res / layout / main.xml and res / layout-land / main.xml to switch from portrait to landscape mode.
What should add or do to resolve this behavior?

Since you have specified the map fragment in the layout file, you can safely find it using findFragmentById in onCreate like so:
MapFragment mMapFragment; // this is neither GoogleMap or MapView!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
initializeMap();
}
#Override
public void onResume() {
super.onResume();
// map init was already done in onCreate, will not do it twice
}
Then when you need the GoogleMap object you ask for it and have it delivered via a callback. When the callback executes, the map is guaranteed to be not null. This will work since Google Play services library v6.5.
private void initializeMap() {
mMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
// googleMap will never be null here
// do not store it in a variable, always make a getMapAsync call
// do your map setup here
}
}
}
This is as quick as it gets so you shouldn't experience any flickering.
Note: In the original post you mentioned you had different layouts for portrait and landscape. If you really do, make sure both contain corresponding fragment IDs, otherwise you're bound to get exceptions. You also mentioned the layout was main.xml not activity_main.xml so make sure you use the layout you intended.

I solved my problem reading this (Activity restart on rotation Android) Activity restart on rotation Android

Related

Android google map disappears/redraws for a second

You might know that the android studio offers a project skeleton for using google maps. This project just contains a single activity and a map fragment. This is my short version of it:
public class MapsActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.wrapper, new SupportMapFragment(), "TEST")
.commit();
}
}
}
With this example I see a "redraw" problem. The map itself disappears for a second (just the map but not the whole map fragment because we can still see the map controls) when:
App is minimized and opened again
When user moves to another activity and moves back again to the map
This was captured on a Nexus4 (4.4.4). However I don't see this issue on another phone that has android 4.1.2.
Any ideas what might be wrong? Are there any solutions for this?
Update
Just found out the google maps app itself has this issue. So this must be a problem with the google maps build on android 4.4.4.
Created a bug report: https://code.google.com/p/android/issues/detail?id=77192&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

Map of MapFragment gets loaded with lag when returning from another activity

As far as I can see, MapFragment has an issue with transition animations. All views on the layout are getting shown immediately, including the MapFragment's own views (like zoom buttons). But the map itself gets loaded with a lag only after the animation is completed.
In order to illustrate the problem, I did the following:
I changed one of the Activities in the Google maps android API examples slightly. It opens a blank activity via an Action Item. When I click back button, the map gets loaded, but only after the transition is completed.
I exaggerated the transition effect a little bit, so that you can see the problem better. I set the transition animation speed in Developer Options to 5x. Even on 1x speed, this lag is disturbing though.
See this video: http://www.youtube.com/watch?v=12SEotktlXI
Do you have any suggestion to prevent this lag? Why do all views get loaded immediately but the map itself doesn't?
Testing environment: Nexus 5, Android 4.4.2, unrooted
Edit: This problem also occures when MapView is used instead of MapFragment.
Reason: Its because as soon as you settings activity will be shown, the map activity will be on its onpause() state; thus, I assume android management reclaimed the memory from the map activity.
Solution:
Make a static class and declare you map statically there, to avoid android from reclaiming the memory used by your map.
Ex.
//your static class
public class MapData{
public static GoogleMap map;
}
//your map activity
public class MapActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(MapData.map != null)
MapData.map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}

Android google map v2 is not working after the application is closed and reopened

When the application opens I create a new instance of a google map during onAttachedToWindow(), this works fine while the application is open. When I close the application and re-open it the google map does not load correctly, it just shows grey tiles.
#Override
public void onAttachedToWindow() {
if (hasCreated()) {
uiGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_content_map_fragment)).getMap();
}
}
When should the google map be initialised to ensure it is loaded when the application is closed and re-opened?
EDIT:
I have tried initialising the map in onResume() and onCreate() and get the same problem
on Google API2 this hassle has been eliminated.
What you need to do is just initialize the map you created on your xml.
uiGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_content_map_fragment))
.getMap();
So when you close and re-open it it will be available. no need to override anything.
I think that the best place would be to create a class that extends SupportMapFragment, and initialise the map in:
#Override
public void onAttach(Activity activity)
Besides that though, I think if you initialise the map in onResume of the container activity it should be ok. But if you by initialising you mean adding the mapfragment to the activity, I would suggest doing it in the xml or in your activity's onCreate. Just my two pennies.

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

Google Maps v2 sometimes resets when resuming app

I am using Google Maps v2 in my Android application. Everything works fine, but if I leave the application (using the home button), use the device for other stuff and then return to my app, the maps just reset themselves and discard all my settings and listeners.
I think it's because my phone doesn't have that much RAM so when it needs it for running other apps, something that's vital for my app gets lost... If I don't use other apps in the meantime, it resumes with all the settings and listeners intact.
So I have a member variable private GoogleMap map = null;
I need to create the map programatically because I am putting it into a LinearLayout (fragment_wrapper) that I also want to use for other fragments. So I am doing this in my onCreate() method:
mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_wrapper, mapFragment);
fragmentTransaction.commit();
The map doesn't get initialized very quickly, so I need to set its settings and listeners in the onResume() method:
if (map == null) {
map = mapFragment.getMap();
UiSettings settings = map.getUiSettings();
settings.setMyLocationButtonEnabled(false);
settings.setZoomControlsEnabled(false);
(...)
map.setOnMapClickListener((...));
(...)
}
I originally put the if block there so that the app wouldn't have to set the map's settings and listeners again on resuming. But then I noticed it forgets the settings. So I thought that if I deleted the if block, it would just set up the map again since onResume() gets called when the app is resumed. But it doesn't work either... So all the map's settings and listeners are gone which renders the map useless and my app needs to be restarted in order to set up the map again.
Can anyone please tell me what the problem could be and how to solve it? Thank you.
I was finally able to find the solution in the Google Maps v2 sample code, which is located at <android-sdk-folder>\extras\google\google_play_services\samples\maps\. See https://developers.google.com/maps/documentation/android/intro#sample_code. What helped me was the file src\com\example\mapdemo\ProgrammaticDemoActivity.java.
What was happening was that the application got shutdown completely, so its onCreate() method was called again. There you have to somehow find out whether you have already created the map fragment or not. The solution is to try to find a tag of your map fragment. If it can't be found, it means the activity is running for the first time. You create your fragment and add the tag to it. Then the next time the onCreate() gets called, your fragment can be found by the tag and all the settings and listeners are preserved.
So you can for example set a final static variable in your activity: private static final String MAP_FRAGMENT_TAG = "map";
and then use this code in your onCreate() method:
mapFragment = (MapFragment) getFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_wrapper, mapFragment, MAP_FRAGMENT_TAG);
fragmentTransaction.commit();
}
setUpMapIfNeeded();
This in the onResume() method:
setUpMapIfNeeded();
And the setUpMapIfNeeded() method could look like this:
if (map == null) {
map = mapFragment.getMap();
if (map != null) {
setUpMap();
}
}
(So the map sets up already in onCreate() and if not, then in onResume().)
Hope this helps someone.

Categories

Resources