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?
onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task.
onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .
After the onResume() method has run, the activity has the focus and the user can interact with it. The onPause() method runs when the activity stops being in the foreground. After the onPause() method has run, the activity is still visible but doesn't have the focus.
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
}
The correct answer is to use Activity
's onRestart()
method. This is probably what you have been looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With