2016-12-31 39 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