I am trying to understand Android Activity lifecycle. For that purpose, I have created Activity where I have overridden all lifecycle methods(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");
}
}
I put breakpoints on the lines where I am logging using Log.d(...). Then I tried to debug. onCreate method is okay - it is calling when activity is created.
Strange situation is starting onStart method. According to Android documentation: "onStart() Called when the activity is becoming visible to the user." But when I debug, it comes to onStart method but Button which is on this Activity, not visible yet.
I thought it supposed to be seen onResume() - after calling onStart() method. But button was not visible.
Only after onResume method, button was visible.
So my question is what is wrong with onStart and onResume methods? Maybe I am doing something not in the way it supposed to be done?
onResume() -> called when the activity is in the foreground, or the user can interact with the Activity. Show activity on this post. onStart() means Activity entered into visible state and layout is created but can't interact with this activity layout. Resume() means now you can do interaction with activity layout.
onStart(): This method is called when an activity becomes visible to the user and is called after onCreate. onResume(): It is called just before the user starts interacting with the application. onPause(): It is called when the app is partially visible to the user on the mobile screen.
The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. For example, this method is where the app initializes the code that maintains the UI.
onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.
No. The onResume() method makes visible of Activity. As you said thanks to the Documentation: "onStart() Called when the activity is becoming visible to the user".
And if you read carefully: "onResume() Called when activity will start interacting with the user."
UPDATE:
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).
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