2012-07-29 18 views
0

我有一個叫做硬幣的int,只要用戶收集一些就會顯示出來。每當顯示活動時它都設置爲零,但我想在另一個頁面上顯示累計結果。這就是我所積累的結果:Android:如何積累保存的int?

if (counter == 0) { 
    totalCoins += coins; 
    counter++; 
     }  

我已經顯示totalCoins但每當應用程序被殺害和恢復,totalCoins重新設置爲0。我已經嘗試使用與共享偏好將其保存方法

private void putValue(String name, int value) { 
     SharedPreferences sp = getSharedPreferences("sharedPreferences", 0); 
     SharedPreferences.Editor prefEditor = sp.edit(); 
     prefEditor.putInt(name, value); 
     prefEditor.commit(); 
    } 

    private int getValue(String name, int defaultValue) { 
     SharedPreferences sp = getSharedPreferences("sharedPreferences", 0); 
     return sp.getInt(name, defaultValue); 
    } 

,然後用putValue("TotalCoins", totalCoins);totalCoins = getValue("TotalCoins", 0);如何積累的硬幣,而硬幣設置爲0,並且還在不斷增加中它時,活動重述?

回答

0

我猜你會來檢查,如果TotalCoins不大於零,之前設置TotalCoins爲零,在顯示活動。(可能在onStart()onResume()