2016-12-31 38 views
0

因此,我有一個活動,用戶可以查看他收集的單詞。如果輸入的單詞存在並僅使用可用的字母,則他/她將得到一個出現在textView中的分數。Android Studio活動 - 使用onCreate(),onResume()等

問題:

  • 當用戶退出的活動(例如去收集更多的字母),然後回到他進入的話活動,目前比分是不再有和所有即使已經使用過他收集的信件也會回到那裏。我知道這個問題是因爲每次用戶離開並進入時都會重置該活動。我只是不太明白如何使用onCreate,onResume,onPause方法。

  • 看來用戶只能輸入一個單詞。當我在輸入第一個單詞後嘗試輸入另一個單詞時,即使他有該單詞可用的字母,也沒有任何反應。

我的代碼具有這種結構目前,

public class calculateScoreActivity extends AppcompactActivity{ 
    //initialise variables to be used 
    public void onCreate(Bundle savedInstanceState){ 
     //set variables to textViews etc 
     //then go to method buttonClicked() 
} 

public void buttonClicked(){ 
    //if the button is pressed and user input is correct go to: 
    updateDictionary() 
    calculateScore() 
} 

public void updateDictionary(){ 
    //remove letters used in the word the user inputted 
} 

public void calculateScore(){ 
    //calculate the user score 
} 

我是正確的寫的onCreate外面這些方法?我在哪裏以及如何使用onPause和onResume,以便用戶可以從他離開的地方拿起?

回答

0

Activity Life Cycle

閱讀此表。使用範圍內的方法,我通常在onCreate()中完成所有靜態設置(視圖e.t.c),然後在需要時使用生命週期的其他方法。

OnPause會在您傳遞到前臺的活動之前,並且當您重新開始此活動時,onResume將被激活。

+0

所以我會通過我所有的計算分數的方法,檢查允許說onResume或onStart等字? – Nicholas

+0

如果您退出活動以獲取返回的內容,請使用onResume,如果您已退出活動以導航到之後使用onStart。 – HeTheMan

+0

[這是對這兩者之間區別的答案](http://stackoverflow.com/questions/4553605/difference-between-onstart-and-onresume) – HeTheMan