【问题标题】:Android Studio activity - using onCreate(), onResume() etcAndroid Studio 活动 - 使用 onCreate()、onResume() 等
【发布时间】:2016-12-31 09:15:33
【问题描述】:

所以,我有一个活动,用户可以查看他收集的单词。如果输入的单词存在并且只使用了可用的字母,那么他/她将获得一个出现在 textView 中的分数。

问题:

  • 当用户退出活动(例如去收集更多的字母)然后回到他输入单词的活动时,当前的分数不再存在,他收集的所有字母都回来了那里即使用过它们。我知道这个问题是因为每次用户离开并进入活动时都会重置活动。我只是不太明白如何使用 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 以便用户可以从他离开的地方继续?

【问题讨论】:

    标签: java android activity-lifecycle


    【解决方案1】:

    Activity Life Cycle

    阅读此图表。使用范围内的方法,我通常在 onCreate() 中进行所有静态设置(视图等),然后在需要时使用生命周期的其他方法。

    OnPause 将在您传递到即将进入前台的 Activity 之前,当您再次进入该 Activity 时将激活 onResume。

    【讨论】:

    • 那么我会通过我所有的方法来计算分数,检查单词等让我们说 onResume 或 onStart 吗?
    • 如果您退出活动以获取某些东西作为回报,请使用 onResume,如果您已退出活动以在之后导航到它,请使用 onStart。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多