Need best practice advice for using several maps in an Android app

3792
22
01-25-2012 05:50 AM
FredrikPaasche
New Contributor
I need to display a map several places in an Android app. My problem is that the first map I create works fine, but the second one will not display the current position or any graphic markers. The maps are in different activities.

I have tried to call map.recycle() when pausing the first activity, and this makes the second map display the graphic markers, but then I need to recreate the first map all over again, which causes me to think that I am going the wrong way about this. 

The code used to create the maps are identical in the two activities, except for the resource IDs which are different.

What is the best way (or correct way) to have two maps active in two different activities in the same app? What should be done in the onCreate, onDestroy, onPause and onResume?

Example code:

// Credentials to access the map
String tokenServiceUrl = "https://somewhere/arcgis/tokens";
String userName = "secret";
String password = "secret";
UserCredentials credentials =  new UserCredentials();
credentials.setTokenServiceUrl(tokenServiceUrl);
credentials.setUserAccount(userName, password);
 
// Initialize the map
map = (MapView) findViewById(R.id.closebyMap);
String mapUrl = "http://path_to_mapserver";
ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(mapUrl, credentials);
map.addLayer(tiledLayer);
graphicsLayer = new GraphicsLayer();
map.addLayer(graphicsLayer);  

Then I set up a setOnStatusChangedListener, wait until I get INITIALIZED, then add graphic markers and start localization. When receiving location changed, I zoom the map to the current location.

Any help will be greatly appreciated!
0 Kudos
22 Replies
DanO_Neill
Occasional Contributor III
This workflow is unsupported as we only support one map running in an application at a time.  This is a known issue with our current release of the SDK and we are looking into supporting this in the future.
0 Kudos
FredrikPaasche
New Contributor
Thank you for the quick response, even though it was bad news for me. I am now trying something different:

I have a front page activity with a listview. Two of the choices lead to activities that causes maps to be displayed. I start the first activity and initialize and display the map. When the user hits the "back"-key I remove/clean up the map in the onStop method, and the front page is displayed again. When the user selects the second choice, I do exactly the same thing with a different map.

I have tried the following in onStop:
LocationService ls = map.getLocationService();
ls.stop();
graphicsLayer.recycle();
map.recycle();

but the app behaves unpredictably. Sometimes it works, sometimes it crashes or hangs with no error messages. Sometimes the recycle() command causes "call to OpenGL ES API with no current context (logged once per thread)" to appear in LogCat.

What's the correct way (if possible) to clean up a map session when the map activity is finished?
0 Kudos
PatrickDoody
New Contributor II
Bumping this last question. What's the proper way to recycle/destroy/free up resources for an ArcGIS Map?

I'm getting lots of weird side effects, even when working with one map - such as when I close my application, then restart it, graphics will no longer be visible on the graphics layer (this problem even exists when there is only one map). In order for the graphics to become visible again, I must close the application, then 'Force Stop' my application and re-open. The graphics will now be visible again.

This perhaps seems like a problem of resources no getting cleaned up? But how should we do so?
0 Kudos
JozefKaslikowski
New Contributor
Bumping this last question. What's the proper way to recycle/destroy/free up resources for an ArcGIS Map?

I'm getting lots of weird side effects, even when working with one map - such as when I close my application, then restart it, graphics will no longer be visible on the graphics layer (this problem even exists when there is only one map). In order for the graphics to become visible again, I must close the application, then 'Force Stop' my application and re-open. The graphics will now be visible again.

This perhaps seems like a problem of resources no getting cleaned up? But how should we do so?


Have you tried invalidating the map upon resume to see if the redraw happens?
0 Kudos
PatrickDoody
New Contributor II
Have you tried invalidating the map upon resume to see if the redraw happens?


Yes, I've tried invalidate and postInvalidate.
0 Kudos
JozefKaslikowski
New Contributor
Yes, I've tried invalidate and postInvalidate.


What about when stepping through, are all of the objects still counted in the GraphicsLayer?

I don't mean to ask basic questions, but I haven't experienced this and I have three such layers at a time.
0 Kudos
PatrickDoody
New Contributor II
What about when stepping through, are all of the objects still counted in the GraphicsLayer?

I don't mean to ask basic questions, but I haven't experienced this and I have three such layers at a time.


No worries! The graphics layer exists, graphics and their symbology are properly created and are contained in that layer. The layer simply will not be visible on the map. This is often triggered when two maps are open in the same application, but I have encountered this numerous times when the second map is not opened at all - however that one is a bit more intermittent. I can reproduce this issue every time using two maps, however with one map it's a bit more random and I've yet to pin it down.
0 Kudos
DanO_Neill
Occasional Contributor III
What's the proper way to recycle/destroy/free up resources for an ArcGIS Map?


You should implement the following Activity methods in the class with your MapView


        MapView map;

        //*******
        //********

        @Override
         protected void onDestroy() {
                    super.onDestroy();
                    map.recycle();
               }

        @Override
         protected void onPause() {
                    super.onPause();
                    map.pause();
               }

       @Override
        protected void onResume() {
                    super.onResume();
                     map.unpause();
               }


We do not support multiple MapViews with this release of the SDK.  This is a limitation of our current implementation and there is no workaround unfortunately.   We are working to resolve this in the next release.
0 Kudos
JozefKaslikowski
New Contributor
We do not support multiple MapViews with this release of the SDK.  This is a limitation of our current implementation and there is no workaround unfortunately.   We are working to resolve this in the next release.


Nevermind, I read the answer. My bad.
0 Kudos