Android - onStart 方法有什么问题?

标签 android android-activity

我正在尝试了解 Android Activity 生命周期。为此,我创建了 Activity,其中覆盖了所有生命周期方法(onCreate、onStart、onRestart、onResume、onPause、onStop、onDestroy):

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.d("ActivityTutorial", "onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("ActivityTutorial", "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("ActivityTutorial", "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("ActivityTutorial", "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("ActivityTutorial", "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("ActivityTutorial", "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("ActivityTutorial", "onDestroy");
    }

}

我在使用 Log.d(...) 记录日志的行上放置了断点。然后我尝试调试。 onCreate 方法没问题 - 它在创建 Activity 时调用。

奇怪的情况是启动 onStart 方法。根据 Android documentation : "onStart() 当 Activity 对用户可见时调用。"但是当我调试时,它涉及到 onStart 方法,但是这个 Activity 上的 Button 还不可见。

enter image description here

我认为应该在调用 onStart() 方法后看到 onResume()。但是按钮不可见。

enter image description here

只有在 onResume 方法之后,按钮才可见。

enter image description here

所以我的问题是 onStart 和 onResume 方法有什么问题?也许我正在做一些不应该做的事情?

最佳答案

没有。 onResume() 方法使 Activity 可见。正如您所说,感谢文档:“当 Activity 对用户变得可见时调用 onStart()”。

如果您仔细阅读:“onResume() 在 Activity 将开始与用户交互时调用。”

更新:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

关于Android - onStart 方法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32945187/

相关文章:

java - 如何将ZXING导入android studio?

android - aSmack 错误 : XMPPConnection is abstract; cannot be instantiated

android - ObjectAnimator.setDuration 被忽略

android - 无论如何要在不执行 StartActivity 的情况下附加到 Activity 堆栈?

Android Lint 提示 Activity 未在 AndroidManifest 中注册,但它是

Android 和 Eclipse - 发生了什么事?

android - Android 2.2 每个应用程序的最大内存限制是多少?

java - 在操作栏选项卡/fragment 之间切换时如何保持状态?

java - 如何在 Activity 中访问 fragment 的按钮

Android 在 Activity recreate() 上进行转换