[ANDROID]APP加载界面完毕时回调,onWindowFocusChanged的使用
package com.example.test_resume; import android.app.Activity; import android.os.Bundle; /** * 在onWindowFocusChanged中操作可以有效避免第一次启动APP和每次从后台启动后加载弹窗时报错Unable to add window * --token null is not valid的bug * * * @author NULL * */ public class MainActivity extends Activity { private boolean isFirstLaunch = false; private boolean isResume = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); isFirstLaunch = true; } @Override protected void onResume() { super.onResume(); isResume = true; } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { if (isFirstLaunch) { isFirstLaunch = false; // TODO 第一次启动界面加载完毕后的操作 } if (isResume) { isResume = false; // TODO 每一次从后台打开后界面加载完毕后的操作 } } } /** * 到后台时要移除添加window的操作 */ @Override protected void onPause() { super.onPause(); } }