2013-03-16 19 views
0

我想盡可能優化我的應用程序。哪個代碼更優化(速度,內存使用情況等),爲什麼?你怎麼看 ;-)?使用後的最終或零視圖?什麼更優化?

1.

final Button testButton = (Button) findViewById(R.id.testButton); 
    testButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
       (some code here) 
     } 
    }); 

2.

Button testButton = (Button) findViewById(R.id.testButton); 
    testButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
       (some code here) 
     } 
    }); 
    testButton = null; 

PS。你有沒有任何Android代碼優化技巧?

+0

[Android應用程序優化]的可能重複(http://stackoverflow.com/questions/3545345/optimization-of-android-applications) – Pragnani 2013-03-16 12:23:48

+0

檢查此http://developer.android.com/training/articles/ perf-tips.html – Pragnani 2013-03-16 12:25:30

+0

謝謝。我讀過它,但我仍然不確定。靜態最終是否更優化? – androfan 2013-03-16 12:33:39

回答

1

我懷疑這是你的應用程序的瓶頸。我根本不擔心這一點。

此外,如果testButton是一個局部變量,它將超出範圍,沒有時方法返回並將其設置爲null將無效。在您的應用程序中使用或不使用final代碼將無法衡量。嘗試製作用戶實際需要的部件,以便更快地等待。

這就是說,我覺得有最終的關鍵字在那裏使代碼更具可讀性。

1

我同意vidstige。

您剛剛將static添加到您的問題中,那在那裏沒有意義。靜態最終意味着僅用於常量。

另外一個方法中的靜態最終變量定義不能編譯!所以跳過它。如果你不打算這樣做,那麼最後的結果就足夠了,基本上是一種你不重用變量的保險。

相關問題