2014年1月18日土曜日

SuefaceViewの起動タイミング

SuefaceViewを使用する際に、SurfaceHolderのCallbackが、どのタイミングで呼ばれるのか気になったので、ちょっと実験してみた。


主要なファイルはMainActivity.javaとTestSurfaceView.java

MainActivity.java(抜粋)

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TestTools.log("MainActivity: onCreate");
  setContentView(R.layout.activity_main);
  FrameLayout container = (FrameLayout)findViewById(R.id.container);
  container.addView(new TestSurfaceView(this));
 }

 @Override
 protected void onResume() {
  super.onResume();
  TestTools.log("MainActivity: onResume");
 }

 @Override
 protected void onStart() {
  super.onStart();
  TestTools.log("MainActivity: onStart");
 }
 
 @Override
 protected void onStop() {
  super.onStop();
  TestTools.log("MainActivity: onStop");
 }
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  TestTools.log("MainActivity: onCreateOptionsMenu");
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  TestTools.log("MainActivity: onDestroy");
 }
 
 @Override
 protected void onPause() {
  super.onPause();
  TestTools.log("MainActivity: onPause");
 }
 
}


TestSurfaceView.java(抜粋)

public class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

 public TestSurfaceView(Context context) {
  super(context);
  TestTools.log("TestSuefaceView: TestSuefaceView()");
  
  SurfaceHolder sh = getHolder();
  sh.addCallback(this);
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  TestTools.log("TestSuefaceView: surfaceChanged");
  TestTools.log("Width: " + width + ", Height: " + height);
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  TestTools.log("TestSuefaceView: surfaceCreated");
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  TestTools.log("TestSuefaceView: surfaceDestroyed");
 }
 
}


実験環境

SDK: Android 4.2.2 (API LEVEL 17)
実験機器: Nexus7(2012)


結果

初回起動時

MainActivity: onCreate
TestSuefaceView: TestSuefaceView()
MainActivity: onStart
MainActivity: onResume
TestSuefaceView: surfaceCreated
TestSuefaceView: surfaceChanged
Width: 1238, Height: 586
MainActivity: onCreateOptionsMenu

画面切り替え時

MainActivity: onPause
TestSuefaceView: surfaceDestroyed
MainActivity: onStop

画面復帰時

MainActivity: onStart
MainActivity: onResume
TestSuefaceView: surfaceCreated
TestSuefaceView: surfaceChanged
Width: 1238, Height: 586

画面回転時

MainActivity: onPause
MainActivity: onStop
MainActivity: onDestroy
TestSuefaceView: surfaceDestroyed
MainActivity: onCreate
TestSuefaceView: TestSuefaceView()
MainActivity: onStart
MainActivity: onResume
TestSuefaceView: surfaceCreated
TestSuefaceView: surfaceChanged
Width: 758, Height: 1055
MainActivity: onCreateOptionsMenu

surfaceCreatedとsurfaceChangedは、毎回一緒に呼ばれるようだ。そして、画面切り替え時や回転時は、必ずsurfaceDestroyedが呼ばれる。surfaceChangedは毎回呼ばれるから、こっちで初期化処理をやってもいいのかな?

今回使ったEclipseプロジェクトファイルはこちら
SurfaceViewLaunchTest.zip






0 件のコメント:

コメントを投稿