Activity的生命周期通常来说或有七个
onCreate()
onStart() onRestart()
onResume()
onPause()
onStop()
onDestroy()
Section1
那么,什么时候可以获取我们的view的宽度呢,我们做个实验
public class MainActivity extends Activity { private TextView tv_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_1 = (TextView) findViewById(R.id.tv_1); Log.i(getClass().getSimpleName(),"onCreate: tv length is==>"+tv_1.getWidth()); } @Override protected void onStart() { super.onStart(); Log.i(getClass().getSimpleName(),"onStart: tv length is==>"+tv_1.getWidth()); } @Override protected void onResume() { super.onResume(); Log.i(getClass().getSimpleName(),"onResume: tv length is==>"+tv_1.getWidth()); } @Override protected void onPause() { super.onPause(); Log.i(getClass().getSimpleName(),"onPause: tv length is==>"+tv_1.getWidth()); } @Override protected void onStop() { super.onStop(); Log.i(getClass().getSimpleName(),"onStop: tv length is==>"+tv_1.getWidth()); } @Override protected void onDestroy() { super.onDestroy(); Log.i(getClass().getSimpleName(),"onDestroy: tv length is==>"+tv_1.getWidth()); } @Override protected void onRestart() { super.onRestart(); Log.i(getClass().getSimpleName(),"onRestart: tv length is==>"+tv_1.getWidth()); } public void goToNext(View view) { startActivity(new Intent(this,NextActivity.class)); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); } }
操作之后打印结果如下
第一次进入时,
09-15 13:55:46.498 30389-30389/com.example.test2 I/MainActivity: onCreate: tv length is==>0
09-15 13:55:46.499 30389-30389/com.example.test2 I/MainActivity: onStart: tv length is==>0
09-15 13:55:46.501 30389-30389/com.example.test2 I/MainActivity: onResume: tv length is==>0
进入下一个界面时
09-15 13:56:53.724 30389-30389/com.example.test2 I/MainActivity: onPause: tv length is==>300
09-15 13:56:54.186 30389-30389/com.example.test2 I/MainActivity: onStop: tv length is==>300
返回时
09-15 13:57:09.321 30389-30389/com.example.test2 I/MainActivity: onRestart: tv length is==>300
09-15 13:57:09.323 30389-30389/com.example.test2 I/MainActivity: onStart: tv length is==>300
09-15 13:57:09.324 30389-30389/com.example.test2 I/MainActivity: onResume: tv length is==>300
退出时
09-15 13:57:28.510 30389-30389/com.example.test2 I/MainActivity: onPause: tv length is==>300
09-15 13:57:28.951 30389-30389/com.example.test2 I/MainActivity: onStop: tv length is==>300
09-15 13:57:28.952 30389-30389/com.example.test2 I/MainActivity: onDestroy: tv length is==>300
可以看到,activity的前三个生命周期,一直到onResume都无法获取view的宽度,后面onpause之后能获取
到view的宽度对于我们来说其实意义不大了。
怎么办?
Section2
我们点开onResume的源码来看看有没有线索
/** * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or * {@link #onPause}, for your activity to start interacting with the user. * This is a good place to begin animations, open exclusive-access devices * (such as the camera), etc. * * <p>Keep in mind that onResume is not the best indicator that your activity * is visible to the user; a system window such as the keyguard may be in * front. Use {@link #onWindowFocusChanged} to know for certain that your * activity is visible to the user (for example, to resume a game). * * <p><em>Derived classes must call through to the super class's * implementation of this method. If they do not, an exception will be * thrown.</em></p> * * @see #onRestoreInstanceState * @see #onRestart * @see #onPostResume * @see #onPause */ @CallSuper protected void onResume() { if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this); getApplication().dispatchActivityResumed(this); mActivityTransitionState.onResume(this, isTopOfTask()); mCalled = true; }
看来我们找到线索了,关键是这句,
Use {@link #onWindowFocusChanged} to know for certain that your * activity is visible to the user
我们修改下代码
public class MainActivity extends Activity { private TextView tv_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_1 = (TextView) findViewById(R.id.tv_1); Log.i(getClass().getSimpleName(),"onCreate: tv length is==>"+tv_1.getWidth()); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv length is==>"+tv_1.getWidth()); } }run以后打印结果如下
09-15 14:09:52.838 13862-13862/com.example.test2 I/MainActivity: onCreate: tv length is==>0 09-15 14:09:52.885 13862-13862/com.example.test2 I/MainActivity: onWindowFocusChanged: tv length is==>300
看来想要获取view的宽度,在
onWindowFocusChanged()中是可以的。