Reputation: 5957
When a GlSurfaceView is embedded in a layout, e.g.,
<FrameLayout
android:id="@+id/framelay"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.nelsondev.myha3ogl.M3View
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
Then when the layout is inflated it gets constructed automatically using the constructor with the signature: GLSurfaceView(Context context, AttributeSet attrs). So it's not literally declared or instantiated in the Activity class.
The Android documentation says that the Activity's onPause/onResume must call the SurfaceView's onPause/onResume. How should I do this? I.e., how can the Activity which inflated the layout get access to the GlSurfaceView object to make those calls?
Edit: This is for Android 2.2
Thanks in advance!
Upvotes: 0
Views: 3427
Reputation: 1973
In your XML layout, give your SurfaceView a name by adding the name attribute:
<com.nelsondev.myha3ogl.M3View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/my_surfaceView1"/>
Next, override onPause and onResume in your activity, find the view by using findViewById(R.id.my_surfaceView1);
and then call onPause and onResume on your surfaceView:
@Override
public void onPause(){
com.nelsondev.myha3ogl.M3View myView = (com.nelsondev.myha3ogl.M3View)findViewById(R.id.my_surfaceView1);
myView.onPause();
super.onPause();
}
Finally, in your implementation of your surface view, override onPause() / onResume() and put any code you need to do when your activity pauses / resumes in there. Remember also to call super.onPause() / super.onResume() in the surface view
findViewById()
method on any ViewGroup object to find child views inside that viewgroup:
MyActivity extends Activity{
public void onCreate(Bundle bundle){
FrameLayout myFrameLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.graphics, null, false);
TextView myView = (TextView)myFrameLayout.findViewById(R.id.textView1);
if(myView!=null){
myView.setText("about to be removed");
myFrameLayout.removeView(myView);
}
setContentView(myFrameLayout);
}
}
Or findViewById()
is also a method in Activity, which will find any view in the layout you set using setContentView();
MyActivity extends Activity{
public void onCreate(Bundle bundle){
setContentView(R.layout.graphics);
// where the xml file in your question is called graphics.xml
com.nelsondev.myha3ogl.M3View myGLSurfaceView = (com.nelsondev.myha3ogl.M3View)findViewById(R.id.my_surfaceView1);
FrameLayout myFrameLayout = (FrameLayout)findViewById(R.id.framelay);
}
}
Upvotes: 2