Android Reusing SurfaceViews from a single layout xml file on two tabs of a tabactivity
I have a layout.xml file with a surfaceview and various buttons.
This is loaded (setContentView) into two instantiations of a class. Each instance is an activity on a different tab (within another class which is a tabactiviy).
The problem: if I start the app with tab1 开发者_如何学Go(setCurrentTab) as the opening tab it (tab1) can draw on the surface. If I set tab2 as the opening tab, it can draw on the surface. BUT switching between the tabs when running, only the initial tab can draw on the surface. It's as if the initial tab has a lock on the surface.
I've tried making the surfaceview invisible (setvisibility) on the inactive tab during pause and resume but this just covers (I think) the other tab's copy of the surfaceview.
The question: can two tabs share a single layout's surfaceview? How can a 'resumed' tab get control from the 'paused' tab?
PS I have rewritten, torn apart, tried reloading (rebuilt in several ways this application over 3 weeks and I get the feeling that there's something about shared layout that I'm missing.
I've cut this cade back to the following and I still have can't get the non-default tab to redraw it's view on moving to its tab (even though some parts, such as a background colour, are updated correctly. The code is below.
I would be extremely grateful for ay advice on getting this to work. It should show "Mode 1" or "Mode 2" depending on the selected tab but, only the tab selected in tabHost.setCurrentTab(1); ever shows the string BUT the red or blue border does update.
// THIS IS THE ENTRY POINT ////////////////////////////////////////
public class TabTest extends TabActivity
{
Intent intent;
Bundle params;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// TEMPLATE
intent = new Intent().setClass(this, TabContent.class);
params = new Bundle();
params.putString("TABNAME", "Mode 1");
intent.putExtras(params);
spec = tabHost.newTabSpec("Template").setIndicator("template",
res.getDrawable(R.drawable.tab_pane))
.setContent(intent);
tabHost.addTab(spec);
// ATTEMPT intent = new Intent().setClass(this, TabContent2.class); params = new Bundle(); params.putString("TABNAME", "Mode 2"); intent.putExtras(params); spec = tabHost.newTabSpec("Attempt").setIndicator("attempt", res.getDrawable(R.drawable.tab_pane)) .setContent(intent); tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
// Tab content one ////////////////////////////////////////
public class TabContent extends Activity { ScreenWrite s=null; SurfaceView screen_1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
Bundle params = this.getIntent().getExtras();
String name = params.getString("TABNAME");
screen_1=(SurfaceView)findViewById(R.id.screen_1);
s=new ScreenWrite((Context)this, screen_1, name);
}
}
// Tab content two ////////////////////////////////////////
public class TabContent2 extends Activity { ScreenWrite s=null; SurfaceView screen_2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.content2);
Bundle params = this.getIntent().getExtras();
String name = params.getString("TABNAME");
screen_2=(SurfaceView)findViewById(R.id.screen_2);
s=new ScreenWrite((Context)this, screen_2, name);
}
}
// The content layouts ////////////////////////////////////////
// and ////////////////////////////////////////
////////////////////////////////////////
PS the layouts were truncated from the submission but are essentially just holders for a background colour and surfaceview. Can supply if not clear
Using SurfaceView is a bit tricky, you might try manually setting e.g:
screen_2.setVisibility(View.GONE)
When it loses focus e.g. via an onTabChangedListener.
精彩评论