Hi Justin,

I too have spent a great deal of time debugging this - with little
luck.
Thanks for taking the time to explain your findings, it does help to
understand what was going on.

In the end I chose to redesign my application and use just the one
MapView - mainly due to the memory issues.

The reason I found myself in this problem was due to the fact I needed
a TabHost and a MapView in the same activity. However I found I
couldn't extend both a TabActivity and MapActivity in the same
Activity so I opted to create my own "TabbedMapActivity", created
using the Android source code.

I explained it all here if you want to see how I achieved this:
http://groups.google.com/group/android-developers/browse_thread/thread/81631e00bd25ee83/8e28418d64337ebb?lnk=gst&q=mscwd01#8e28418d64337ebb

Ultimately, I believe you are right, having multiple MapViews will
inevitable lead to memory issues regardless of how careful you are to
save map states and recycle resources; so I think using just the one
MapView is the only answer at this moment in time.

Thanks

On Dec 9, 11:49 pm, "Justin (Google Employee)" <[EMAIL PROTECTED]>
wrote:
> Once upon a time I spent a lot of time debugging this issue, so here
> is what I found.
>
> ***WARNING: This information was valid on an internal version of the
> SDK between M5-RC15 and 0.9 beta, it may no longer be valid, test
> thoroughly and proceed with  caution.***
>
> First, some information about MapView and MapActivity. Each Android
> process may have multiple MapViews and MapActivitys, but they all
> manipulate a single MapView backing object. This is because MapViews
> take a *huge* amount of memory, roughly half of the 16MB allowed to
> each application. This *also means* you need to be very careful about
> your memory usage when using MapView. Make sure to use DDMS to analyze
> memory usage to make sure you don't run out of space. Because a single
> view backs all map views in an application, you also need to save and
> restore the map state (including center position and zoom) when your
> MapActivity subclass pauses/resumes.
>
> For the purposes of discussion I will refer to two activities,
> ActivityAlpha and ActivityBeta, each is a subclass of MapActivity and
> has a MapView. ActivityAlpha starts ActivityBeta. The user will
> eventually return to ActivityAlpha either via the 'back' button or
> ActivityBeta finishing naturally.
>
> When ActivityBeta finishes, there are two issues that I believe causes
> the problem you see:
>
> 1) onCreate is not called on the launching ActivityAlpha, onResume is
> 2) a certain UI focal change doesn't occur
>
> Certain initialization of the MapView and various helper entities are
> done by MapActivity when onCreate is called. When ActivityBeta is
> launched, onPause is called on ActivityAlpha and the helpers for the
> MapView are stopped. onResume does some of the restarting, but clearly
> not all that is necessary. This can be worked around by calling
> super.onCreate(null) from your onResume or onActivityResult method of
> your launching MapActivity subclass. ***WARNING***: This is
> potentially very dangerous, MapActivity may not expect to be abused
> this way, test thoroughly and proceed with caution.
>
> The second issue results in the above hack not quite working. When
> ActivityBeta finishes, and the above hack is applied, ActivityAlpha is
> ready to load tiles, but it won't until the MapView is touched so that
> the MapView checks to see if it needs tiles. So far, I don't have a
> workaround for this, possibly a programmatic pan or zoom would cause
> the MapView to load any missing tiles. Some of my previous testing
> also indicated that any missing tiles would load when ActivityAlpha
> resumes if you use startActivity() to start ActivityBeta instead of
> startActivityForResult().
>
> The bottom line is that maps were never designed to have multiple map
> views in a single application, they're very heavy objects. Hopefully
> this helps and is still valid information, let me know how it goes.
>
> Cheers,
> Justin
> Android Team @ Google
>
> On Dec 8, 5:39 pm, mscwd01 <[EMAIL PROTECTED]> wrote:
>
> > Having tried the test you suggested it seems I can go back and forth
> > between my Activity1 map and the inbuilt Google Maps app perfectly
> > fine - no memory worries. However my application, constantly throws up
> > OutOfMemory errors.
>
> > One thing I have noticed is after Activity1 calls Activity2, Activity1
> > is never able to load additional tiles. It seems Activity2, even if it
> > is destroyed after pressing the back button and returning to Activity1
> > always retains some kind of "focus" - If you reopen Activity 2, the
> > map is able to load new tiles. Very strange...
>
> > On Dec 8, 11:47 pm, Mark Murphy <[EMAIL PROTECTED]> wrote:
>
> > > mscwd01 wrote:
>
> > >  > Hope I havent confused you ;)
>
> > > No, but you have made my head hurt.
>
> > >  > Oh btw I purchased your "The Busy Coder's Guide to Android
> > >  > Development" book - its very helpful ;)
>
> > > My head feels better now... ;-)
>
> > > > However what i'm asking is, is there a way to kill off Activity2
> > > > completely when pressing the Back button to return to Activity1 and
> > > > return full control to the first map?
>
> > > Well, you already tried the stock answer of using finish(). With your
> > > kill-my-process hack, you're heading down in a direction which, on my
> > > map of Android, is labeled "Here There Be Dragons"...
>
> > >  > I believe I am making more and
>
> > > > more map objects when I launch Activity2 again and again and not
> > > > releasing resources when I finish with it?
>
> > > Or they're just not getting garbage collected fast enough or something.
>
> > > I have a suggestion. I have no idea how crazy it will be for you to
> > > utilize if it works.
>
> > > In Activity1, temporarily replace launching Activity2 with a
> > > startActivity() that launches the built-in Maps application. You should
> > > be able to do this through:
>
> > > startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("geo:40,-75")));
>
> > > which will give you a map of the NJ side of the Philadelphia suburbs.
>
> > > Now, try bouncing back and forth between your Activity1 and the Maps
> > > application, like you were bouncing between Activity1 and Activity2.
>
> > > If things fail as before, I think you may just be screwed, at least in
> > > terms of rapidly flipping between two MapView instances. They may make
> > > too much garbage.
>
> > > If, however, this holds up, it may be you can only effectively have one
> > > MapView per *application* if you want to rapidly switch between them. In
> > > that case, your mission (should you choose to accept it) is to split
> > > your project into two separate applications. This would suck mightily,
> > > particularly if you were looking to deploy via a market, but it's
> > > possible it is the best way for you to get two stable MapViews under
> > > your control.
>
> > > If it were me, I'd try very very hard to get by with one map, somehow.
>
> > > --
> > > Mark Murphy (a Commons Guy)http://commonsware.com
> > > _The Busy Coder's Guide to Android Development_ Version 1.9 Available!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to