Android onCreate onResume

你说的曾经没有我的故事 提交于 2019-11-28 02:42:51

问题


I have a problem. When I start for the first time my android application, in the main activity both the onCreate and the onResume are called. but I want to be called only the onCreate.

What can I do?


回答1:


According to the SDK docs what you are seeing is the intended behavior. Have a look at the flowchart in the docs for Activity - Activity Lifecycle.

Programmatically you can overcome this by keeping an instance member to track whether onResume has been called before - the first time it is called, set the variable and return e.g.

private boolean resumeHasRun = false;

@Override
protected void onResume() {
    super.onResume();
    if (!resumeHasRun) {
        resumeHasRun = true;
        return;
    }
    // Normal case behavior follows
}



回答2:


The correct answer is to use Activity's onRestart() method. This is probably what you have been looking for.




回答3:


You can't do anything, as this is how the Activity lifecycle works.

See http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle for a diagram that shows the lifecycle.




回答4:


As you can see in the API the Activity Lifecycle always calls onResume before showing the activity. http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

I guess you could make a global boolean for a first access and set it to false. Then override the onResume and check the variable. If false, set it to true and return, if true, call super.onResume.

Should work, but I don't know if it can be handled simpler and I don't have access to the sdk here to test it.



来源:https://stackoverflow.com/questions/6175679/android-oncreate-onresume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!