Q:

SurfaceView inside TabHost causes crash

I have a SurfaceView-derived class in an Activity inside a TabHost (like the HelloTabWidget sample). The SurfaceView I'm using comes with the excellent andrtoid-flot-chart library: FlotChartContainer.java
When I switch between the tabs, everything works. However, as soon as the tab containing this view has been initialized (e.g. shown) my application is doomed. It will crash whenever the following occurs:
Pressing Home button
When I re-open my application, it crashes due to an IllegalStateException in surfaceCreated().
Starting new activity
When I start a new activity (startActivity) and press the Back button (this invokes my SurfaceView's surfaceDestroyed...?!), it crashes due to an IllegalStateException in surfaceCreated().

I'm targeting API level 7 (Android 2.1).
The code works when TabHost is not used.
Switching between tabs does not invoke surfaceDestroyed(). Instead, onPause() and onResume() are used.
When an activity is finished, surfaceDestroyed() is called even if it is not in the active tab.

Please help!

A:

After experimenting with onPause(), onResume(), surfaceChanged() and  surfaceDestroyed() I finally came up with an ugly solution that solves this particular case:

No XML! View created programmatically.
Re-create and re-add the view in onResume().
Remove the view in onPause().

@Karthi_heno
You pointed me in the right direction. The key is to handle onPause() properly. However, I ran into weird delays (some 10 seconds, probably due to Canvas.lockCanvas() calls)...

