關於android activity載入完畢事件

來源:互聯網
上載者:User

很多人都想在activity載入完畢的同時處理一下事件,如彈出一個popWindow或者其他,但在onCreate方法中處理這個時間的話,程式會報錯:大意是activity還沒有載入完,不能處理彈窗事件,其實不光是彈窗,只要用到activity.this的事件,在activity載入完之前都不能處理,原因很明顯——popWindow或者是dialog的顯示都要依附在一個parent Activity上,所以在Activity載入完之間,他們都不能顯示。

不過萬幸的是android給開發人員提供了一個方法,可以解決這個問題,這個方法就是onWindowFocusChanged()

 

其說明如下:

Called when the current Window of
the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user. The default implementation clears the key tracking state, so should always be called.

Note that this provides information about global focus state, which is managed independently of activity lifecycles. As such, while focus changes will generally have some relation to lifecycle changes (an activity that is
stopped will not generally get window focus), you should not rely on any particular order between the callbacks here and those in the other lifecycle methods such as onResume().

As a general rule, however, a resumed activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows
have it. Likewise, the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.

Java代碼  

  1. @Override  
  2.     public void onWindowFocusChanged(boolean hasFocus)  
  3.     {  
  4.         if (hasFocus)  
  5.         {  
  6.         }  
  7.     }  

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.