activity的外部即時重新整理問題

來源:互聯網
上載者:User

很多人看到這個問題就會想到不就是用ContentObserver嗎

但ContentObserver並沒想象的那麼好,因為還沒做到精確到某一列  某一行的資料

所以會出現介面的頻繁重新整理,或者是頻繁的判斷改變資料的內容。

用application域和類似反向控制的做法就可以很好的解決這個問題

application類
view plain
public class ApplicationContext extends Application { 
    public static final int FLUSH_MAIN_ACTIVITY=1; 
    private Handler main; 
    public void setMainHandler(Handler handler){ 
        main=handler; 
    } 
     
    public void flushMain(){ 
        main.sendEmptyMessage(FLUSH_MAIN_ACTIVITY); 
    } 
 

activity類裡要實現的內部handler類
view plain
Handler flushHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
        switch (msg.what) { 
        case ApplicationContext.FLUSH_MAIN_ACTIVITY: 
            initData(); 
            initView(); 
            break; 
        default: 
            break; 
        } 
        super.handleMessage(msg); 
    } 
}; 

activity裡的OnCreate方法裡只要設定好handler類就可以了
view plain
appC = (ApplicationContext) getApplication(); 
appC.setMainHandler(flushHandler); 


調用的時候更簡單 直接調用application的sendEmptyMessage方法就可以了

作者“獨自登高樓 望斷天涯路”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.